Skip to the page

State: snapshots, updates and where state lives · mid

Context and its re-render cost

Context passes a value deep into the tree without props; every component that reads it re-renders when the value changes by Object.is, so a new object on every render, or one big context for everything, re-renders the whole app.

The notes

What you must know

  • Context is dependency injection, not a state manager: it moves a value; the state still lives in a component (or a store).
  • All consumers re-render when the Provider value changes by Object.is. value={{ user, theme }} creates a new object every render, so every consumer re-renders even if nothing changed.
  • React.memo does not stop a context-triggered re-render: a component that reads the context re-renders anyway.
  • Fixes: memoize the value with useMemo; split contexts by domain and by how often they change; put state and dispatch/setters in separate contexts (setters are stable).
  • For frequently changing shared state (cart, live prices) use an external store with selectors (useSyncExternalStore, Zustand, Redux) so each component subscribes only to the slice it needs.
  • React 19: render <ThemeContext value={...}> directly; <Context.Provider> still works but will be deprecated. use(ThemeContext) can read context after an early return.
  • React 19.3: Server Components can render a Context imported from a "use client" file directly, but still cannot create context or read it.
  • Context is great for low-frequency values: theme, locale, signed-in user, feature flags.

The red pen

Common traps: mistake → fix

  • Mistake: "Wrap consumers in memo to stop context re-renders"

    Fix: consumers re-render whenever the value they read changes.

  • Mistake: "One AppContext for user, theme and cart is simpler"

    Fix: adding to the cart re-renders everything that reads theme.

  • Mistake: "The React Compiler fixes context re-renders"

    Fix: it memoizes values and JSX; it does not give context selectors.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is Context for, and is it a state manager?
  2. We put user, theme, cart and setCart in one context and the whole app re-renders on add-to-cart. Why, and how do you fix it?
  3. Does memo stop context-triggered re-renders?
  4. How do Zustand or Redux avoid this problem?
  5. What changed with Context in React 19 and 19.3?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview