React Reference ยท Concepts and Pitfalls
Context performance
Why every React context consumer re-renders when the value changes, and how to avoid it: split contexts, stable values, children props and external stores.
When a context provider receives a new value, React re-renders every component that reads that context, no matter how deep it is and no matter whether the part of the value it uses has changed. Parents wrapped in memo do not stop it, because the consumer is notified directly. Context has no selector, so a component cannot subscribe to "just user.name".
For small apps this is fine. In a large tree, one big context that changes often (a form state, a mouse position, a timer) can trigger a wave of re-renders. The cure is to shape your contexts so a change reaches only the components that need it.
const SettingsContext = createContext(null); <SettingsContext value={{ theme, user }}>{children}</SettingsContext> // A new object on every render: all consumers re-render each time.
Try it
Click the button and watch the console: UserBadge renders again even though the user never changed and the component is wrapped in memo.
Now a click logs only ThemeButton rendered. UserBadge reads a different context, and children is the same element object as before, so React does not re-render it.
Techniques
- Split by update frequency. Keep things that change together in one context and things that change at different rates in separate ones: for example,
ThemeContext(rarely changes) andCursorContext(changes constantly). - Separate state from actions.
dispatchand setter functions are stable. Put them in their own context (as above, or via a reducer'sdispatch) so components that only trigger updates never re-render because of state changes. - Keep the value stable. If the provider re-renders for other reasons,
value={{ a, b }}creates a new object each time and notifies all consumers. Memoize it withuseMemowhen the provider can re-render without a real change. - Put state inside the provider and render
children. Elements passed aschildrencome from the parent and keep their identity when the provider's own state changes, so only consumers re-render, not the whole subtree. Do not hold the state higher up and re-create the children each time. - Move state down. If only one part of the tree needs a fast-changing value, keep it in a local component instead of a global context.
- Wrap expensive consumers. A consumer can read context in a thin wrapper and pass the needed slice as props to a memoized child. The wrapper re-renders, but the heavy child bails out if the slice did not change.
- Use an external store for fine-grained updates.
useSyncExternalStoreor a library with selectors (Zustand, Redux Toolkit, Jotai) lets each component subscribe to exactly what it reads.
Usage notes
- Comparison uses
Object.is. A primitive value that stays equal does not notify; a new object or array with the same content does. - Default values apply without a provider. The
defaultValueofcreateContextis only used when there is no matching provider above. - Do not guess; measure. Use the React DevTools Profiler (see
Profiler) to see which components render after a context change before restructuring. - Context is not state management. It transports a value; the state still lives in
useStateoruseReducer. - The React Compiler memoizes values and components automatically, but it cannot make a consumer skip a context change; splitting still matters.
- Strict Mode doubles render logs in development; count renders in production builds or with the Profiler.