Course contentsShow
← React
Lesson 813 of 2,39621. Context FundamentalsFree lesson

When NOT to Use Context: Frequent Updates

Understand why rapidly-changing state in context causes performance problems.

When NOT to Use Context: Frequent Updates

What you'll learn: Why putting rapidly-changing state into context creates performance bottlenecks in your React apps.

The Performance Trap

Context is amazing for infrequent updates like theme switches or user authentication changes. But when state changes rapidly—think mouse coordinates, form input, or animation frame updates—context becomes a performance nightmare.

Here's the problem: every component that consumes a context re-renders whenever that context's value changes. If your context value updates 60 times per second (like a cursor position tracker), every single consumer re-renders 60 times per second—even if they don't care about the specific data that changed.

Why This Happens

Unlike state management libraries with subscription mechanisms or React's built-in optimizations for local state, context has no way to selectively notify components. It's all-or-nothing. Change one property in your context object? Every useContext call triggers a re-render.

Real-World Examples to Avoid

❌ Form input state updating on every keystroke
❌ Mouse/touch coordinates for drag-and-drop
❌ Scroll position tracking
❌ WebSocket messages arriving continuously
❌ Animation frame data

For these scenarios, use local state with useState, useReducer, or consider external state libraries designed for fine-grained subscriptions.

The Analogy

Think of context like a fire alarm in a building. When it goes off, everyone evacuates—perfect for rare, critical events. But imagine if it rang every time someone typed a letter in an office. The entire building would constantly evacuate for no reason. That's what frequent context updates do to your component tree.

Key Takeaway: Context is ideal for infrequent, global updates. For rapidly-changing state, keep it local or use specialized state management tools with selective subscriptions.