Skip to main content

Choosing Zustand, Redux Toolkit, TanStack Query, or SWR

Zustand​

Use for compact shared client state with selector-based subscriptions. Define store creation carefully for SSR; avoid a module singleton when request-specific data could leak between users.

Redux Toolkit​

Use when the product needs explicit event-driven transitions, middleware, normalized client entities, devtools history, or organization-wide conventions. Keep server-authoritative data out of Redux unless there is a deliberate synchronization design.

TanStack Query and SWR​

Use for client-side server state: polling, focus revalidation, optimistic mutations, offline behavior, or data that must update without a server navigation. They complement Server Components; they do not replace server-side authorization.

Decision table​

NeedDefault choice
Shareable filters/paginationURL state
Database-backed page dataServer Component
Small interactive widgetuseState / useReducer
Stable dependency across subtreeContext
Lightweight shared client stateZustand
Complex event-driven client domainRedux Toolkit
Client polling/offline/live cacheTanStack Query or SWR

Hydration pattern​

Pass only serializable, non-sensitive initial data into a client provider. Treat it as a snapshot, not an authorization proof. Mutations return to the server, re-check identity and permissions, then invalidate the relevant server and client caches.

Anti-patterns​

  • Duplicating the same entity in Router Cache, query cache, and global store without an ownership rule.
  • Hydrating secrets or entire database rows into browser state.
  • Creating request-specific stores at module scope on the server.
  • Using optimistic UI without reconciliation and rollback.
  • Invalidating every query after every mutation.