← Back to React, page 3

react-0045 · Hard senior

Why can React context cause unnecessary re-renders, and how do you structure providers to avoid it?

Every component that calls useContext for a context re-renders when that context's value changes, even if it uses only part of the value. Avoid it by splitting contexts (one for state, one for dispatch, one per concern), keeping the provider's value stable, passing children through the provider so they do not re-render with it, and moving fast-changing state into a store with selectors when context is not enough.

Full answer

Context has no selector. If the value is { user, theme, notifications } and only notifications changes, every consumer re-renders, because React compares the whole value with Object.is and sees a different object.

Two separate causes hide inside this problem. First, a new value object on every provider render, such as value={{ a, b }}, re-renders all consumers even when a and b did not change. Second, a provider that holds state re-renders its own subtree when the state changes, unless the subtree is passed as children.

The remedies, in order of effort:

  1. Keep the value stable. Memoize it with useMemo, or use a value that is already stable.
  2. Pass children. function Provider({ children }) renders <Ctx value={...}>{children}</Ctx>. The children elements are created by the parent of the Provider, so they are the same on a state change and React skips re-rendering them. Only the consumers re-render.
  3. Split by change frequency and by role. A common pattern keeps state in one context and the dispatch function in another. Dispatch from useReducer is stable, so components that only trigger actions never re-render when state changes.
  4. Split by domain: separate AuthContext, ThemeContext and CartContext.
  5. If many components need slices of fast-changing state, use a store with selectors (Zustand, Redux Toolkit, Jotai) or build one on useSyncExternalStore. Components subscribe to a slice and re-render only when that slice changes.

React.memo on consumers does not help against context changes: a context update bypasses memo. And the compiler does not change the rule either, because it concerns the value changing.

The example logs which components render. A button that only dispatches does not re-render when the count changes.

jsx
import { createContext, useContext, useReducer } from 'react';

const CountStateContext = createContext(0);
const CountDispatchContext = createContext(null);

function reducer(count, action) {
  if (action === 'add') return count + 1;
  throw new Error('Unknown action');
}

function CountProvider({ children }) {
  const [count, dispatch] = useReducer(reducer, 0);
  return (
    <CountStateContext value={count}>
      <CountDispatchContext value={dispatch}>{children}</CountDispatchContext>
    </CountStateContext>
  );
}

function CountDisplay() {
  const count = useContext(CountStateContext);
  console.log('CountDisplay rendered');
  return <p>Count: {count}</p>;
}

function AddButton() {
  const dispatch = useContext(CountDispatchContext);
  console.log('AddButton rendered');
  return <button onClick={() => dispatch('add')}>Add</button>;
}

export default function App() {
  return (
    <CountProvider>
      <CountDisplay />
      <AddButton />
    </CountProvider>
  );
}

State and dispatch live in two contexts, and the components are passed as children. After one click only the component that reads the count renders again. Console output (the first two lines are the mount): CountDisplay rendered AddButton rendered CountDisplay rendered

Key points

  • All consumers re-render when the context value changes, with no selector support.
  • Do not create the value object inline on every provider render.
  • Pass children to the provider so unrelated subtrees are not re-rendered with it.
  • Split state and dispatch, and split by domain and update frequency.
  • Use a store with selectors for fast-changing shared state.

Common mistakes

  • Putting the whole application state in one large context.
  • Creating a new value object in the provider on every render.
  • Expecting React.memo on a consumer to stop context-driven re-renders.
  • Placing a fast-changing value such as pointer position in context.
  • Rendering the subtree inside the same component that owns the state instead of passing children.

Follow-ups

  • How does useSyncExternalStore help build a selector-based store?
  • Why does the dispatch function from useReducer have a stable identity?
  • When is context still the right tool compared with Zustand or Redux Toolkit?

Tips

  • Open with "no selectors". It names the root cause precisely.
  • Explain the children trick. Few candidates know it.
  • Close with the limit: context for slow data, a store for fast data.