Trivia Questions1 min readPremium

Avoiding Context Re-render Pitfalls

Context is easy to adopt and easy to misuse. The most common production problem is a single “app context” whose every update re-renders half the tree. Here are the patterns that fix it. Each fix below reduces React context re-render cost, and the React docs on useContext explain why consumers re-render.

1. The problem

const AppContext = createContext(null);

function AppProvider({ children }) {
  const [user, setUser] = useState(null);
  const [cart, setCart] = useState([]);
  const [theme, setTheme] = useState('light');
  return (
    <AppContext.Provider value={{ user, setUser, cart, setCart, theme, setTheme }}>
      {children}
    </AppContext.Provider>
  );
}

Two issues:

  • The value object is recreated on every render, so consumers re-render even when nothing changed.
  • Adding an item to the cart re-renders every component that reads user or theme from this context.

2. Fix 1: memoize the value

const value = useMemo(() => ({ user, setUser }), [user]);

This stops re-renders caused by the Provider’s parent re-rendering, but not those caused by unrelated fields changing.

3. Fix 2: split contexts by concern

One context per independently-changing piece of state: AuthContext, CartContext, ThemeContext. A cart update now only re-renders cart consumers.

4. Fix 3: separate state from actions

Setters from useState and dispatch from useReducer are stable. Put them in their own context so components that only trigger updates never re-render when the state changes.

const CartStateContext = createContext(null);
const CartDispatchContext = createContext(null);

function CartProvider({ children }) {
  const [state, dispatch] = useReducer(cartReducer, initialCart);
  return (
    <CartDispatchContext.Provider value={dispatch}>
      <CartStateContext.Provider value={state}>{children}</CartStateContext.Provider>
    </CartDispatchContext.Provider>
  );
}
// <AddToCartButton> reads only CartDispatchContext -> never re-renders on cart changes

5. Fix 4: selectors via an external store

Context cannot subscribe to part of a value. When many components read small slices of frequently-changing state, use a store with selector subscriptions (Zustand, Redux, Jotai, or your own store built on useSyncExternalStore). Components re-render only when their selected slice changes.

const count = useCartStore((s) => s.items.length); // re-renders only when length changes

A common pattern is to put the store instance (which never changes) in context and subscribe with selectors.

6. Keep providers low

If only one page needs a provider, mount it on that page instead of the app root. Fewer consumers means fewer re-renders and less memory.

React Context Re-render Checklist

  1. Memoize provider values.
  2. Split contexts by concern and by change frequency.
  3. Put stable actions in their own context.
  4. Use selector-based stores for hot, widely-read state.
  5. Mount providers as low in the tree as possible.