Performance: Multiple state variables
MEDIUM IMPACT
This affects how React updates the UI and manages rendering performance when state changes.
const [count, setCount] = useState(0); const [text, setText] = useState(''); function updateCount() { setCount(prev => prev + 1); } function updateText(newText) { setText(newText); }
const [state, setState] = useState({ count: 0, text: '' }); function updateCount() { setState(prev => ({ ...prev, count: prev.count + 1 })); } function updateText(newText) { setState(prev => ({ ...prev, text: newText })); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Single large state object | One update triggers full component re-render | Multiple reflows if many DOM nodes depend on state | Higher paint cost due to full update | [X] Bad |
| Multiple independent state variables | Updates trigger minimal re-rendering | Fewer reflows limited to changed parts | Lower paint cost with targeted updates | [OK] Good |