Skip to main content

Combining state tools without duplicating state

Large React applications often use more than one state tool.

That is healthy when each tool owns a different category.

It becomes dangerous when the same value is copied into several stores.

The ownership ruleโ€‹

For every value, choose one authoritative owner.

One value, one authoritative owner
Server record
Authoritative on the server; the query cache holds a snapshot.
Form draft
Authoritative in the form control while the user is editing.
URL filter
Authoritative in the URL when navigation/shareability matters.
Local dialog
Authoritative in component state.

Other layers may derive or display the value, but they should not silently become competing owners.

Good compositionโ€‹

Product edit page: clear ownership by lifecycle
URL
Owns productId.
TanStack Query
Owns the saved Product snapshot from the server.
React Hook Form
Owns the unsaved product draft.
Context
Provides locale / permissions environment.
useState
Owns delete-confirmation dialog state.

Each layer has a distinct responsibility.

Bad composition: copying server data everywhereโ€‹

Anti-pattern: serially copying one value into multiple owners
TanStack Query cache
Redux
Component state
Form

Now four versions may disagree.

Better:

Better: derive for display, fork only when lifecycle changes
TanStack Query cache
Saved server snapshot remains the authoritative remote value.
React Hook Form draft
Unsaved edits are intentionally separate because they have a different lifecycle.

The form draft is intentionally separate because unsaved edits are a different lifecycle.

Query data โ†’ form defaultsโ€‹

A common pattern:

const profileQuery = useQuery({
queryKey: ['profile', userId],
queryFn: () => getProfile(userId),
})

When the edit workflow starts, initialize the form from the saved record.

1saved profile snapshot
2form default values
3user edits independent draft

Do not automatically reset the form every time the query background-refetches while the user is typing.

You need a product rule for reconciling remote changes with local unsaved edits.

React Hook Form + TanStack Query mutationโ€‹

Clean form + server mutation hand-off
React Hook Form validates
TanStack mutation
Error
Map domain/server errors back to the form UI.
Success
Update/invalidate query cache and reset the form to saved values.

This is a common and clean separation.

Redux Toolkit + TanStack Queryโ€‹

If Redux owns shared client workflow state and TanStack Query owns server state:

Redux + TanStack Query without duplication
Redux Toolkit

Checkout step ยท client-side workflow flags ยท unsaved cross-route decisions.

TanStack Query

Products ยท inventory ยท orders.

Avoid copying every query result into Redux.

If the application already uses Redux extensively, evaluate RTK Query as an alternative server-state layer before adding a second query cache.

Zustand + TanStack Queryโ€‹

A diagram/editor application might use:

Zustand + TanStack Query in an editor
Zustand

Selected nodes ยท zoom ยท active tool ยท drag state ยท unsaved interaction state.

TanStack Query

Document record ยท collaborators ยท server history.

The UI can combine both without one replacing the other.

Context + external storeโ€‹

Context can inject a specific store instance into a subtree.

Provider-scoped external store instance
Store factory
Provider chooses instance
Feature subtree
External store selectors

This is useful when you want external-store selector semantics but still need provider-scoped instances, testing isolation, or SSR request isolation.

URL + storeโ€‹

Do not keep two synchronized copies of a filter unless necessary.

Bad:

Anti-pattern: URL/store synchronization bridge
URL page=2
Effect sync โ†”
Every bridge creates another failure mode.
Redux page=2

Every synchronization bridge introduces failure modes.

If the page number is navigable/shareable, read/write the URL as the source of truth.

A client store may still own temporary UI that does not belong in navigation history.

Persistence + server stateโ€‹

Persisting a query result or global store does not make it authoritative.

Persistence does not change authority
localStorage
Client-controlled cached/persisted snapshot.
Server
Authoritative domain record.

When restoring persisted state:

  • validate shape/version;
  • consider expiration;
  • avoid secrets;
  • reconcile with current server state when required.

Migration: Context โ†’ Zustandโ€‹

A safe migration can use an adapter period.

Context โ†’ Zustand migration without dual ownership
Old Context consumers
Provider reads the Zustand store
Consumers migrate gradually

Important: do not let Context and Zustand independently mutate separate copies.

One owner; temporary adapters only.

Migration: Context โ†’ Redux Toolkitโ€‹

1define target Redux domain slice
2migrate transition logic
3expose selectors/actions
4old provider adapts to Redux
5migrate consumers
6delete adapter state

Avoid a dual-write period where both stores pretend to be authoritative.

Migration: Redux โ†’ Zustandโ€‹

Do not translate slices mechanically.

First classify state again.

You may discover:

Reclassify before migrating Redux
Server records
โ†’ TanStack Query
URL filters
โ†’ router
Form draft
โ†’ React Hook Form
Shared client state
โ†’ Zustand

A migration is an opportunity to fix ownership, not merely change syntax.

Migration: manual API state โ†’ TanStack Queryโ€‹

Legacy architecture:

Legacy manual API lifecycle
useEffect

Request + loading + error + retry + stale flag + refresh function.

Migration:

1define query key
2define query function
3move cache/freshness policy
4migrate mutation/invalidation
5delete duplicated loading/cache state

Do one domain at a time.

Migration: global form draft โ†’ React Hook Formโ€‹

If Redux/Zustand currently owns every field:

1identify why form data was global
2keep only state that must outlive form
3move active draft into RHF
4initialize from authoritative workflow state
5write back only at explicit save boundaries

This often reduces update volume and coupling.

State ownership documentโ€‹

For complex systems, maintain a small architecture table:

StateOwnerLifetimePersistenceConsumers
product recordTanStack Query/servercache/serverserverproduct UI
edit draftRHFedit sessionoptional draftform
active workspaceURLnavigationURLroute/page
editor selectionZustandeditor sessionnocanvas/sidebar
localeContextapp/subtreepreferencemany descendants

This is more useful than a generic rule saying "we use Redux for state."

Failure-mode reviewโ€‹

For every bridge between tools, ask:

Review every synchronization edge
Owner A changes, B does not
Hydration restores stale B
Background refetch arrives during editing
Navigation changes the URL
Server rejects optimistic client state

If the answer requires many synchronization Effects, reconsider ownership.

Staff-level decision frameworkโ€‹

A strong architecture minimizes synchronization edges.

Why fewer sources of truth are easier to operate
Fewer sources of truth
Fewer synchronization bridges
Fewer race conditions
Easier debugging

The goal is not to minimize the number of libraries at all costs.

The goal is to make ownership obvious.

Interview questionsโ€‹

Mid-level: Is using Redux and TanStack Query together automatically redundant?

Senior: Why is copying query data into a global client store often a smell?

Senior: How would you migrate Context state to Zustand without dual ownership?

Staff: You inherit an app where URL params, Redux, Zustand, and component state all contain the same filters. How do you redesign it while keeping the product working during migration?

Summaryโ€‹

State composition summary
Classify state
Choose one owner
Other tools derive or observe
Synchronize only where necessary
Avoid dual writes

Referencesโ€‹