0
0
Reactframework~8 mins

Context provider in React - Performance & Optimization

Choose your learning style9 modes available
Performance: Context provider
MEDIUM IMPACT
This affects interaction responsiveness and rendering speed by controlling how many components re-render when context changes.
Sharing global state with many components
React
const MyContext = React.createContext();

function App() {
  const [count, setCount] = React.useState(0);
  const value = React.useMemo(() => ({ count, setCount }), [count, setCount]);

  return (
    <MyContext.Provider value={value}>
      <ComponentA />
      <ComponentB />
    </MyContext.Provider>
  );
}
Memoizing the context value prevents unnecessary re-renders of consumers when unrelated state changes occur.
📈 Performance GainReduces re-renders to only when count changes, improving INP and reducing CPU work.
Sharing global state with many components
React
const MyContext = React.createContext();

function App() {
  const [count, setCount] = React.useState(0);
  const value = { count, setCount };

  return (
    <MyContext.Provider value={value}>
      <ComponentA />
      <ComponentB />
    </MyContext.Provider>
  );
}

// ComponentA and ComponentB consume context
// but value object is recreated on every render
The context value object is recreated on every render causing all consumers to re-render even if count did not change.
📉 Performance CostTriggers re-render of all consumers on every parent render, increasing INP and CPU usage.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Context value recreated every renderMany re-renders of consumersMultiple reflows if layout changesHigh paint cost due to frequent updates[X] Bad
Memoized context value with stable referenceRe-renders only on actual value changeMinimal reflowsLower paint cost[OK] Good
Rendering Pipeline
When context value changes, React schedules re-render for all consuming components. If the value changes frequently or unnecessarily, it causes many re-renders, increasing layout and paint work.
React Reconciliation
Layout
Paint
⚠️ BottleneckReact Reconciliation due to excessive re-renders triggered by context value changes
Core Web Vital Affected
INP
This affects interaction responsiveness and rendering speed by controlling how many components re-render when context changes.
Optimization Tips
1Memoize context value objects to avoid unnecessary re-renders.
2Split context into smaller providers to limit consumer updates.
3Avoid passing new object or function references as context value on every render.
Performance Quiz - 3 Questions
Test your performance knowledge
What causes unnecessary re-renders in React context consumers?
ARecreating the context value object on every render
BUsing useMemo to memoize the context value
CPassing primitive values as context
DUsing React.StrictMode
DevTools: React DevTools Profiler
How to check: Record a profiling session while interacting with the app. Look for frequent re-renders of context consumers when no visible changes occur.
What to look for: Excessive re-renders of components consuming context indicate poor context value memoization.