0
0
Reactframework~8 mins

Multiple state variables in React - Performance & Optimization

Choose your learning style9 modes available
Performance: Multiple state variables
MEDIUM IMPACT
This affects how React updates the UI and manages rendering performance when state changes.
Managing component state for multiple independent values
React
const [count, setCount] = useState(0);
const [text, setText] = useState('');

function updateCount() {
  setCount(prev => prev + 1);
}

function updateText(newText) {
  setText(newText);
}
Each state variable updates independently, so only the parts depending on that state re-render.
📈 Performance Gainreduces unnecessary re-renders, improving interaction responsiveness (INP)
Managing component state for multiple independent values
React
const [state, setState] = useState({ count: 0, text: '' });

function updateCount() {
  setState(prev => ({ ...prev, count: prev.count + 1 }));
}

function updateText(newText) {
  setState(prev => ({ ...prev, text: newText }));
}
Updating one property causes the entire state object to update, triggering re-render and re-calculation for all state parts.
📉 Performance Costtriggers full component re-render on any state change, increasing INP and CPU usage
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Single large state objectOne update triggers full component re-renderMultiple reflows if many DOM nodes depend on stateHigher paint cost due to full update[X] Bad
Multiple independent state variablesUpdates trigger minimal re-renderingFewer reflows limited to changed partsLower paint cost with targeted updates[OK] Good
Rendering Pipeline
When a state variable updates, React schedules a re-render of the component. Multiple independent state variables allow React to isolate updates and avoid re-rendering unrelated parts.
Reconciliation
Commit phase
Paint
⚠️ BottleneckReconciliation phase when large state objects cause more diffing and re-rendering
Core Web Vital Affected
INP
This affects how React updates the UI and manages rendering performance when state changes.
Optimization Tips
1Split state into multiple variables for independent updates.
2Avoid storing unrelated data in one big state object.
3Use React DevTools Profiler to monitor re-render performance.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a performance benefit of using multiple state variables in React?
AOnly the parts of the component depending on changed state re-render
BAll state updates batch into a single re-render automatically
CIt reduces the bundle size of the app
DIt prevents any re-rendering of the component
DevTools: React DevTools Profiler
How to check: Record interactions and look at component re-render counts and durations when updating state variables.
What to look for: Excessive re-renders of components when only one state variable changes indicates poor state splitting.