0
0
Reactframework~8 mins

Sharing state between components in React - Performance & Optimization

Choose your learning style9 modes available
Performance: Sharing state between components
MEDIUM IMPACT
This affects interaction responsiveness and rendering speed when components update shared data.
Sharing state between sibling components
React
const CountContext = React.createContext();

function Parent() {
  const [count, setCount] = React.useState(0);
  return (
    <CountContext.Provider value={{ count, setCount }}>
      <ChildA />
      <ChildB />
    </CountContext.Provider>
  );
}

function ChildA() {
  const { setCount } = React.useContext(CountContext);
  return <button onClick={() => setCount(c => c + 1)}>Increment</button>;
}

function ChildB() {
  const { count } = React.useContext(CountContext);
  return <div>{count}</div>;
}
Using context avoids prop drilling and allows components to subscribe only to needed state, reducing unnecessary re-renders.
📈 Performance GainReduces re-renders to only components consuming changed state, improving INP and CPU efficiency.
Sharing state between sibling components
React
function Parent() {
  const [count, setCount] = React.useState(0);
  return (
    <>
      <ChildA count={count} setCount={setCount} />
      <ChildB count={count} />
    </>
  );
}

function ChildA({ count, setCount }) {
  return <button onClick={() => setCount(count + 1)}>Increment</button>;
}

function ChildB({ count }) {
  return <div>{count}</div>;
}
Passing state and setters down multiple levels causes all children to re-render on any state change, even if they don't need to update.
📉 Performance CostTriggers re-render of all children on each state update, increasing INP and CPU usage.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Prop drilling shared stateMany props passed downMultiple re-renders on updateHigh paint cost due to many updates[X] Bad
Context API with focused stateMinimal prop passingRe-renders only subscribed componentsLower paint cost[OK] Good
Global state with full object updatesBroad state accessTriggers many re-rendersHigh paint and layout cost[X] Bad
Split contexts and memoized callbacksScoped updatesMinimal re-rendersLow paint and layout cost[OK] Good
Rendering Pipeline
When shared state updates, React schedules re-renders for components consuming that state. Excessive or broad state sharing causes many components to re-render, increasing layout and paint work.
JavaScript Execution
Style Calculation
Layout
Paint
⚠️ BottleneckJavaScript Execution and Layout due to many components re-rendering and recalculating styles.
Core Web Vital Affected
INP
This affects interaction responsiveness and rendering speed when components update shared data.
Optimization Tips
1Avoid passing shared state through many component layers to prevent excessive re-renders.
2Use React Context or state management libraries to scope state updates efficiently.
3Memoize callbacks and split state to minimize components re-rendering unnecessarily.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a common performance issue when sharing state by passing props down many component levels?
AState updates are delayed by network latency.
BAll child components re-render on any state change, even if they don't use that state.
CThe browser blocks rendering until all props are passed.
DCSS styles are recalculated only once.
DevTools: React DevTools Profiler
How to check: Open React DevTools, go to Profiler tab, record while interacting with components that share state, then analyze which components re-render and how long they take.
What to look for: Look for unnecessary re-renders of components that do not depend on changed state, and long render durations indicating performance bottlenecks.