الانتقال إلى المحتوى
FrontendState Management· 6 دقائق للقراءة

Context Is Not a State Manager: How Context Updates Re-render Consumers

Context delivers one value to a subtree, and every reader re-renders when it changes. Split it by how it changes, or use a store with selectors.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • React 19
محتويات المقال

An online shop's category page shows 48 products, each with its own "Add to cart" button. Every click makes the page hitch for a moment. The React Profiler shows why: after each click, all 48 buttons render again, though none of them displays anything about the cart. The only component that needed to change was the item count in the header. Every button calls useContext(CartContext) to get dispatch, and that same context also carries the items.

The common misconception

The belief: context is React's built-in state manager. Put shared state in a provider, read it anywhere with useContext, and React re-renders only the components that read the part of the value that changed, the way a store with selectors would.

It's easy to believe because context does replace a lot of what people used stores for. It ends prop drilling, React's docs show reducer and context together as a way to manage state that distant components need, and a small app with a theme and a signed-in user never notices a cost. Reading const { dispatch } = useContext(CartContext) even looks like a subscription to one field. It isn't.

What actually happens

Context delivers a value; it doesn't manage it

lets a component make a value available to any component below it, however deep. That's the whole feature. The state behind the value still lives in useState or useReducer inside the provider component, and updating it is ordinary React state: the provider re-renders and passes down a new value. Context adds no update API, no selectors and no middleware. In React's own pattern, the reducer manages the state and context delivers it.

Every reader gets the whole value

React's docs describe the rule: when a provider receives a value that's different under Object.is, React re-renders every component that reads that context, starting from the provider, and memo doesn't stop it. React doesn't track which fields a component uses. Destructuring dispatch happens after the read, so a button that only needs dispatch is subscribed to the items too.

An earlier article showed an inline object creating a new context value on every render. useMemo fixes that, and the useContext docs recommend it. But memoization can't help when the data inside really changed: a memoized { items, dispatch } is still a new object every time an item is added, so every reader renders.

Split the value along how it changes

A context can't select, so you narrow updates by giving each kind of value its own context. React's reducer-and-context guide already uses two: one for the state and one for dispatch. The useReducer docs say dispatch has a stable identity, so a context that holds only dispatch never gets a new value, and components that only send actions never re-render because of it:

TSX Cart.tsx
import { createContext, useContext, useMemo, useReducer } from "react";import type { ReactNode } from "react";const CartContext = createContext<CartValue | null>(null);export function CartProvider({ children }: { children: ReactNode }) {  const [items, dispatch] = useReducer(cartReducer, []);  // Memoized, yet a new object every time items change  const value = useMemo(() => ({ items, dispatch }), [items]);  return <CartContext value={value}>{children}</CartContext>;}export function AddToCartButton({ id }: { id: string }) {  const { dispatch } = useContext(CartContext)!; // renders on every add  return (    <button onClick={() => dispatch({ type: "add", id })}>Add to cart</button>  );}

Now an add changes only CartContext, so only its readers, like the header count, render. The 48 buttons read CartDispatchContext, whose value is the same function every time, and they stay still. One condition: the product grid must reach the provider as children, so the provider's own re-render doesn't drag it along.

The same idea scales to other splits. Values that change on every keystroke shouldn't share a context with the theme or the signed-in user, which change rarely and are read almost everywhere.

When you need a real store

Splitting has a limit. When many components each need a different slice of large, frequently changing data, you'd end up with a context per field. That's the job of a store that lives outside React, where each component subscribes to the slice it selects. React's hook for subscribing to one is useSyncExternalStore: when the store changes, it re-renders the component only if the snapshot it returns differs under Object.is. Store libraries offer hooks built on the same idea. React Redux's docs say useSelector compares the selected result with === after each action and re-renders only when it changed, so a selector that builds a new object every time re-renders every time.

ContextA store with selectors
Where the state livesuseState or useReducer in the providerIn the store, outside React
What a component subscribes toThe whole valueThe slice it selects
Who re-renders on a changeEvery reader of that contextReaders whose selected slice changed
A good fit forValues that change rarely or are read togetherBusy shared data read in many small slices

How to apply it in your project

  1. Profile one updateRecord a single interaction in the React DevTools Profiler. Look for context readers that rendered but show nothing that changed.
  2. Map each context's fieldsFor each context, write down what's in its value, how often each part changes, and which components read which part.
  3. Split by rate of changeSeparate state from dispatch, and fast-changing values from stable ones. Memoize any object value that remains.
  4. Move busy, sliced data to a storeWhen splits multiply and many components each read a small part of frequently changing data, use a store with selectors.

Key takeaways

  • Context delivers one value to a subtree; the state behind it is ordinary useState or useReducer in the provider.
  • When that value changes, every reader re-renders, whatever fields it uses, and memo doesn't stop it.
  • useMemo stops new values on unrelated renders, but not when the data inside really changed.
  • Narrow updates by splitting contexts, state from dispatch; for many readers of busy data, use a store with selectors.

📚 المصادر

  1. useContext — React API Reference
  2. Scaling Up with Reducer and Context — React
  3. useReducer — React API Reference
  4. useSyncExternalStore — React API Reference
  5. Hooks: useSelector — React Redux

التعليقات (0)