# The Context Performance Trap

> Why you should never use React Context for rapidly changing data, and how it can silently destroy your app's performance.

Subject: React | Lesson 22 of 48 | 5 min read
Interactive lesson: https://codewithanimation.in/react/context-performance-trap
Previous: [The Context API](https://codewithanimation.in/react/context-api.md) | Next: [Custom Hooks](https://codewithanimation.in/react/custom-hooks.md)

## The Re-render Rule

Whenever the `value` prop of a `<Context.Provider>` changes, React completely bypasses the normal rendering flow.

It searches the entire component tree and **forces every single component that calls `useContext()` to re-render immediately.**

Crucially, it does this even if you try to stop it with `React.memo` or `shouldComponentUpdate`. Context updates are a sledgehammer.

## The Performance Trap

Imagine you build an `AppGlobalContext` and you store the user's current scroll position in it, so a specific nav bar can change color.

Because the scroll position updates 60 times a second, the Context `value` changes 60 times a second.

This forces every single component that consumes `AppGlobalContext` (which might be 50 different components) to re-render 60 times a second. Your app will grind to a halt.

## How to fix it

**1. Split your Contexts:** Don't put everything into one giant `GlobalContext`. Put the `theme` in `ThemeContext` and the `user` in `UserContext`. This way, a theme change doesn't re-render components that only care about the user.

**2. Keep fast data out:** Never put rapidly changing data (scroll position, mouse coordinates, ticking clocks, real-time sockets) into Context. Use a specialized global state manager like Zustand, Redux, or Jotai for that.

## Common mistake: The Object Mutation Bug

Even if your data isn't changing rapidly, you can accidentally trigger this trap by passing a *new object reference* on every render.

`<Provider value={{ user, theme }}>`

Because `{ user, theme }` creates a brand new object literal every time the parent renders, React thinks the Context value changed, forcing all consumers to re-render unnecessarily. Always wrap provider objects in `useMemo`!

## Tip: Third-party libraries

If Context is so dangerous for performance, how do tools like Redux work? They don't put the actual *data* in Context. They put a stable *store reference* in Context, and use a separate system (like `useSyncExternalStore`) to subscribe to fine-grained updates without triggering massive re-renders.

## Try it: The Re-Render Cascade

Turn on the fast-updating state. Notice how every single component that consumes the context flashes red, indicating a forced re-render, even if it doesn't care about that specific piece of data!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/context-performance-trap

## Check yourself

### Question 1

What happens when a Context Provider's value changes?

- A. Only the components whose specific properties changed will re-render.
- B. Every single component that calls `useContext()` for that specific Context is forced to re-render.
- C. React compares the old and new context values and only re-renders if the UI would change.

Correct answer: B

Context updates are forceful and bypass normal rendering bail-outs. Every consumer re-renders.

- Why not A: Context does not have fine-grained property selection. It's all or nothing.
- Why not C: React does not diff the UI before forcing the render.

### Question 2

Why is `<Provider value={{ count, setCount }}>` a potential performance trap?

- A. Because it creates a brand new object reference on every parent render, forcing all consumers to re-render even if count didn't change.
- B. Because `setCount` is an asynchronous function.
- C. Because you are not allowed to pass objects to a Provider.

Correct answer: A

React checks if the value changed using Object.is(). A new object literal `{{}}` is always considered a different value than the previous object literal.

- Why not B: State setters are perfectly safe to pass.
- Why not C: You can pass objects, you just need to ensure their reference is stable.

### Question 3

What is the best way to handle global state that changes 60 times a second (e.g., mouse coordinates)?

- A. Put it in a Context Provider but wrap the consumers in `React.memo`.
- B. Do not use Context. Use a dedicated state management library like Zustand or Jotai that supports fine-grained subscriptions.
- C. Use `useRef` inside the Context Provider.

Correct answer: B

Context is fundamentally the wrong tool for rapidly changing data because of how aggressively it forces consumer re-renders.

- Why not A: `React.memo` cannot stop a Context update.
- Why not C: Updating a ref won't trigger any renders at all, so the UI would never update.

## Remember this

- Context forces EVERY consumer to re-render when the value changes.
- Never put rapidly changing data in Context.
- Split large contexts into smaller, specialized contexts.
- Always wrap object values in `useMemo` before passing them to a Provider.
