Course contentsShow
← React
Lesson 829 of 2,39622. Context PerformanceFree lesson

Context Ignores React.memo

Learning why React.memo cannot prevent re-renders triggered by context changes in consuming components.

Context Ignores React.memo

You'll learn why wrapping a component in React.memo doesn't prevent re-renders caused by context changes.

The Surprising Limitation

You've learned that React.memo prevents unnecessary re-renders when props haven't changed. It's a powerful optimization tool — but it has a blind spot: context updates bypass React.memo entirely.

When a component consumes context with useContext, any change to that context's value will trigger a re-render, even if the component is wrapped in React.memo. The memoization only compares props, not context values.

Why This Happens

React.memo creates a wrapper that checks: "Did my props change?" If not, it reuses the previous render output. But context updates flow through React's internal subscription system — they're not props, so React.memo never sees them.

Think of it like a building's fire alarm system. React.memo is a doorman checking guest lists (props), but the fire alarm (context) bypasses the doorman and alerts everyone inside directly.

The Practical Impact

const ExpensiveChild = React.memo(({ name }) => {
  const theme = useContext(ThemeContext);
  // Even though props (name) haven't changed,
  // this re-renders when ThemeContext updates
  return <div>{name} in {theme.mode}</div>;
});

The component re-renders whenever theme changes, regardless of the React.memo wrapper. The prop comparison is irrelevant once context is involved.

What This Means for Performance

If you're using context for frequently-changing values, React.memo won't save your consumers from re-rendering. You'll need different optimization strategies (which you'll learn in upcoming lessons about splitting contexts and selector patterns).

Key Takeaway: React.memo only prevents re-renders from prop changes; context updates always trigger re-renders in consuming components, bypassing memoization completely.