Performance: Re-rendering behavior
MEDIUM IMPACT
This affects how often React updates the UI, impacting interaction responsiveness and CPU usage.
const Child = React.memo(function Child() { console.log('Child rendered'); return <div>Child component</div>; }); function Parent() { const [count, setCount] = React.useState(0); return ( <div> <button onClick={() => setCount(count + 1)}>Increment</button> <Child /> </div> ); }
function Parent() { const [count, setCount] = React.useState(0); return ( <div> <button onClick={() => setCount(count + 1)}>Increment</button> <Child /> </div> ); } function Child() { console.log('Child rendered'); return <div>Child component</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Parent state update triggers all children re-render | Many virtual DOM diffs | Multiple reflows if DOM changes | Higher paint cost due to updates | [X] Bad |
| Using React.memo to prevent unnecessary child re-renders | Minimal virtual DOM diffs | Single or no reflows if no DOM changes | Lower paint cost | [OK] Good |
| Passing new inline functions as props causing re-renders | Virtual DOM diffs triggered by prop changes | Reflows triggered if DOM updates | Increased paint cost | [X] Bad |
| Memoizing functions with useCallback to stabilize props | Reduced virtual DOM diffs | Fewer reflows | Reduced paint cost | [OK] Good |