Performance: State re-render behavior
HIGH IMPACT
This affects how often React updates the UI, impacting interaction responsiveness and rendering speed.
function Counter() { const [count, setCount] = React.useState(0); function increment() { setCount(prev => prev + 2); } return <button onClick={increment}>{count}</button>; }
function Counter() { const [count, setCount] = React.useState(0); function increment() { setCount(count + 1); setCount(count + 1); } return <button onClick={increment}>{count}</button>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple state updates per event | Multiple state queues, 1 virtual DOM diff | Single reflow | Normal paint cost | [X] Bad |
| Single functional state update | One virtual DOM diff | Single reflow | Lower paint cost | [OK] Good |
| State updates with same value | Equality check performed | No reflows | No paint | [X] Bad |
| Avoid state update if value unchanged | No virtual DOM diff | No reflow | No paint | [OK] Good |
| Parent state update without memo | All children re-render | Multiple reflows | High paint cost | [X] Bad |
| Memoized child components | Selective re-render | Minimal reflows | Reduced paint | [OK] Good |