Performance: useCallback hook
MEDIUM IMPACT
This affects interaction responsiveness by preventing unnecessary function recreations and re-renders in React components.
function Parent() { const [count, setCount] = React.useState(0); const handleClick = React.useCallback(() => { console.log('Clicked', count); }, [count]); return <Child onClick={handleClick} />; }
function Parent() { const [count, setCount] = React.useState(0); const handleClick = () => { console.log('Clicked', count); }; return <Child onClick={handleClick} />; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Passing new function each render | Causes child re-render and possible DOM updates | Multiple reflows if child updates layout | Higher paint cost due to re-render | [X] Bad |
| Memoizing function with useCallback | Child re-renders only when dependencies change | Fewer reflows as layout stable | Lower paint cost due to fewer updates | [OK] Good |