Overview
Curated: · Written: · Reviewed:
React State Management
In one line
Decide who owns a value, how long it lives, and who may write it, then pick the lowest tool that answers that. Use local state or lifting for values one subtree needs, Context for read-mostly values such as theme or current user, an external store for client state that updates often or has many readers, and a query library for anything fetched over HTTP.
Key definitions
- Ownership: the component allowed to change a value. Context changes the read path, not the owner.
- Lowest common owner: the nearest ancestor that every reader sits under. It owns the state.
- Server state: data owned by a backend. It is asynchronous, shared, and stale on arrival.
- Selector: a function that picks the slice of a store a component subscribes to.
- Referential stability: the value keeps the same identity under
Object.isacross renders. - Tearing: a concurrent render showing two components different versions of one store.
useSyncExternalStoreprevents it.
How it works
- A provider publishes
valueand every consumer re-renders when that value failsObject.is. A parent re-render with a new inline object counts as a change. - An external store notifies subscribers, and each
useStore(selector)re-renders only when its selected result changes. - A query cache keys data by query, deduplicates concurrent requests, revalidates in the background, and is invalidated after mutations.
When to use and when not to
| Situation | Tool | Why |
|---|---|---|
| One component reads and writes it | useState or useReducer | The lowest owner is the component |
| Two siblings need it | Lift to parent | Owner is explicit, no new dependency |
| Deep tree, read-mostly, low churn | Context | Built in, cheap, fine for theme or locale |
| Same value, updates every frame | External store or CSS variables | Context re-renders every consumer per update |
| Many writers, complex transitions, time travel | Redux Toolkit | Conventions and devtools justify the ceremony |
| Small global client state | Zustand | Small core, selector subscriptions |
| Anything fetched over HTTP | TanStack Query, RTK Query, SWR | Caching, deduplication, and revalidation are the job |
| Derived from props or state | Compute during render | No state, no sync bug |
Traps interviewers probe
- Inline Context value.
value={{ user, setUser }}re-renders every consumer on each parent render. Fix: memoize the value, or split the setter into its own context so it never changes. - Selector returning a new object.
useSelector(s => ({ a: s.a, b: s.b }))re-renders on every dispatch. Fix: passshallowEqualoruseShallow, or select each field separately.React.memoon children does not stop a context update. - Derived state in
useState. Copyingitemsinto state with auseEffectrenders a stale value first. Fix: computefilteredduring render. - Mutating state in place. Writing
state.items.push(x)outside an Immer draft keeps the same reference, so selectors see no change and the screen stays stale. Fix: return a new reference, or usecreateSlice, which drafts with Immer. - Context as a high-frequency store. Mouse position or scroll offset in Context re-renders every consumer per event. Fix: an external store read through selectors.
Interview tips
Open with ownership and lifetime: name the lowest common owner, how long the value lives, how often it changes, and how many components read it. Then name the tool.
Expected follow-ups:
- A theme read by 200 components and a cursor read by 3: same tool? No. The re-render rule decides it, not the library.
- Why
React.memodoes not stop context updates. staleTimeversusgcTimein TanStack Query: one controls freshness, the other how long unused data stays cached.- Why a module-level store can leak one user's state into another's response during SSR. Fix: a store factory with one instance per request.
30-second outline: one sentence on ownership and lifetime. One sentence on update frequency and reader count. The tool choice and why. One re-render trace, such as the inline value that re-renders all consumers and how memoizing or splitting fixes it. One trade-off, such as Zustand's smaller surface against Redux's enforced conventions.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.