Q&A-1How do you decide whether a piece of state should be local, lifted to a parent, or global?
Answer
The 90-second answer. Three questions, in order: who reads it, who writes it, how often it changes. State belongs to the closest common ancestor of every component that reads it: that is the ownership rule, and it is what "lift state up" means. Update frequency then picks the mechanism, because the cost of a state update is its re-render fan-out: React re-renders the component that owns the state and, by default, its whole subtree. A context value change re-renders every consumer, memo or not. So a value changing 120 times a second and read by 400 components is a different design problem from a theme that changes twice a session, even though both look "global".
| State | Readers | Writes | Home |
|---|---|---|---|
| dropdown open | the dropdown | rare | useState in the dropdown |
| selected row id | table + detail pane | rare | lift to their common parent |
| theme, locale | whole app | rare | context |
| cart lines | header, list, checkout | ~1/interaction | external store with selectors |
| order list from API | several screens | refetch | server cache (TanStack Query), not a store |
| cart total | header | — | derive during render, never store |
Lifting, then the two escapes from prop drilling.
// React 18. Lowest common ancestor owns the state.
function SearchPage() {
const [query, setQuery] = useState('');
return (<>
<FilterPanel query={query} onQueryChange={setQuery} />
<Results query={query} />
</>);
}
When the LCA is far above the readers, context removes the plumbing. Pass children from above so the provider's own state change does not re-render the tree it wraps:
function ThemeProvider({ children }) {
const [theme, setTheme] = useState('dark');
return <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>;
}
// <ThemeProvider><App /></ThemeProvider> — App's element is stable, so it bails out.
When the value is both widely read and frequently written, context is the wrong tool: every consumer re-renders on every write. Move it to an external store and subscribe to a slice, so a component re-renders only when its selection changes:
const total = useStore(s => s.cart.total); // re-renders only when total changes
Failure modes, and how you catch each.
- High-frequency state in context. A drag handler writing pointer position to a context read by 400 components: at 120 Hz that is 48,000 component renders per second (hypothetical, but the arithmetic is the point). Detect: React DevTools Profiler shows a commit on every
pointermovewith the whole consumer list in the flame chart. Fix: keep it local, or use a store with selectors. - Prop drilling through six layers. Detect: components that accept props they only forward and never read. Fix: lift to the LCA, use context for the plumbing, or pass
childrenso the middle layers never see the value. - Server data in a global store. Detect: hand-written invalidation code, and lists that go stale after a mutation elsewhere. Fix: a server cache owns it; the store owns client state only.
- Duplicated state. Two
useStateholding the same fact drift as soon as one update path misses the other. Detect: a bug that only appears after a specific navigation order. Fix: one owner, everything else derived during render.
When not to lift. If only one component reads and writes it, lifting it is a regression: you have widened the re-render surface and added props for nothing. Colocate first; lift only when a second reader appears. And when the identity of the thing changes (a different user, a different draft) reset with key rather than syncing state in an effect.
Curated: · Written: · Reviewed: