Course contentsShow
← React
Lesson 2243 of 2,39649. Performance: Identifying ProblemsFree lesson

Object Props in Context Providers

The classic `value={{ state, dispatch }}` mistake that re-renders all consumers on every parent render.

Object Props in Context Providers

You'll learn: Why the classic value={{ state, dispatch }} pattern in Context providers causes every consumer to re-render on every parent render — and how to spot this in your app.

The Classic Context Provider Mistake

When you write a Context provider like this:

function MyProvider({ children }) {
  const [state, setState] = useState(initialState);
  
  return (
    <MyContext.Provider value={{ state, setState }}>
      {children}
    </MyContext.Provider>
  );
}

You're creating a new object literal on every render. Even if state and setState haven't changed, the object { state, setState } is a brand-new reference each time. React compares values by reference for objects, so every consumer of MyContext sees a "different" value and re-renders — even if the actual data inside is identical.

Why This Hurts Performance

Imagine a form with 20 fields, each using your context. Every keystroke in one field triggers a parent re-render, creating a new context value object, causing all 20 fields to re-render. The Profiler will show cascading renders from the Provider down through all consumers, even those displaying unrelated data.

Spotting It in DevTools

In the Profiler's "why did this component render" output, you'll see context consumers updating because "the context value changed," but when you inspect the actual state values, they're unchanged. The object wrapper is the culprit.

Key Takeaway: Inline object literals in Context Provider value props create new references every render, triggering unnecessary re-renders in all consumers. Always extract or memoize the value object.