Performance: Updating state
HIGH IMPACT
This affects how quickly the UI updates after user interaction and how smoothly the app responds to input.
const [state, setState] = useState({ count: 0, text: '' }); function updateBoth() { setState(prev => ({ ...prev, count: prev.count + 1, text: 'Updated' })); }
const [count, setCount] = useState(0); const [text, setText] = useState(''); function updateBoth() { setCount(count + 1); setText('Updated'); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple separate state updates | Multiple updates to virtual DOM | Multiple reflows triggered | Higher paint cost due to repeated updates | [X] Bad |
| Single batched state update | One update to virtual DOM | Single reflow triggered | Lower paint cost with one update | [OK] Good |