Community JavaScript Snippet

Why My Context Provider Was Re-rendering Everything

We added a flame-graph and saw every consumer of `<UserContext>` re-rendering on every keystroke in a sibling. The fix took two days to isolate and three lines to ship.

Why My Context Provider Was Re-rendering Everything

We added a flame-graph and saw every consumer of `<UserContext>` re-rendering on every keystroke in a sibling. The fix took two days to isolate and three lines to ship.

JavaScript
Frontend
3 snippets
react
hooks
memoization
performance-optimization
amiraprice

By @amiraprice

May 1, 2026

·

Updated May 18, 2026

538 views

5

4.4 (12)

The trap is that JSX hides the allocation. Writing <UserContext.Provider value={{ user, mutate }}> looks declarative, but { user, mutate } is a fresh object literal on every render and React's context machinery uses Object.is to decide whether to fan out an update. The harness above subscribes one consumer and counts how many times it re-runs; we get three updates from three unrelated parent renders even though user was identical. In a real app this manifests as a sluggish form because the search input drags every consumer along for the ride.