Skip to main content
15

Local state, Context, a store or server state: how do you choose?

MediumReactState management

How do you choose between local state, Context, a global store (Redux, Zustand) and server state (TanStack Query)?

TL;DR

Start by asking what kind of state it is. Server state (data from an API) belongs in a cache library like TanStack Query or your framework's loaders, which handle caching, refetching and deduping. UI state stays local (useState, useReducer) and is lifted only as high as needed. Context is for values many components read but that rarely change (theme, locale, current user). A global store is for complex client state shared across distant parts of the app with frequent updates.

Key points

  • Most "global state" in older apps was really server state. Moving it to TanStack Query removes most Redux code.
  • URL state (filters, tabs, pagination) belongs in the URL so it's shareable and survives a refresh.
  • Context isn't a state manager. It's dependency injection; every consumer re-renders when the value changes.
  • Zustand and Jotai are small, hook-based, and re-render only subscribers of the slice that changed.
  • Redux Toolkit fits large teams that want strict patterns, middleware and time-travel debugging.
  • Forms usually want a form library (React Hook Form) rather than global state.

Example

jsx
// Server state
const { data: user, isPending } = useQuery({ queryKey: ['user', id], queryFn: () => getUser(id) })

// Shared client state with Zustand
const useCart = create((set) => ({
  items: [],
  add: (item) => set((s) => ({ items: [...s.items, item] })),
}))
const count = useCart((s) => s.items.length) // re-renders only when length changes

Common follow-ups

  • When is Context enough? Low-frequency values like theme, auth and feature flags.
  • What's prop drilling, and is it always bad? Passing props through layers that don't use them. A few levels is fine; composition (passing children) often fixes the rest.

What interviewers look for

Tip

Classify the state first (server, URL, local, shared client), then pick the tool, and know why Context alone can cause re-render problems.

  • Red flag: putting fetched API data into Redux by hand, or one giant Context for the whole app.

Was this helpful?

Related questions