The Object-Value Reference Trap in Providers
You'll learn why creating fresh objects or arrays in your Provider's value prop causes every consumer to re-render unnecessarily, even when the actual data hasn't changed.
The Problem
When you pass an object or array directly to a Context Provider's value prop, React creates a new reference on every render of the Provider component. Even if the contents are identical, JavaScript treats {user: "Alice"} and {user: "Alice"} as different objects.
Since Context uses referential equality to detect changes (essentially oldValue === newValue), a new object reference signals "the value changed!" — triggering re-renders in every consuming component.
Why This Happens
// ❌ Creates new object on EVERY render
<UserContext.Provider value={{user: currentUser}}>
{children}
</UserContext.Provider>
Every time this Provider component renders (for any reason), it creates a brand new object. All consumers see this as a value change and re-render, even if currentUser itself hasn't changed.
This is the "reference trap" — you're creating referential instability while the data remains stable.
The Real Cost
Even with React.memo on consumer components, they'll re-render because their context dependency genuinely changed (from React's perspective). You've accidentally opted out of all optimization.
The Profiler will show cascading renders spreading from the Provider through every consumer, with "Context changed" as the reason.
Key Takeaway: Creating object or array literals directly in a Provider's value prop causes all consumers to re-render on every Provider render, regardless of whether the actual data changed. Always stabilize the reference with useMemo or extract it outside the component.