Performance: Creating custom hooks
MEDIUM IMPACT
Custom hooks affect component rendering speed and bundle size by reusing logic efficiently and avoiding unnecessary re-renders.
function useCounter() { const [count, setCount] = useState(0); useEffect(() => { const id = setInterval(() => setCount(c => c + 1), 1000); return () => clearInterval(id); }, []); return count; } function Component() { const count = useCounter(); return <div>{count}</div>; }
function Component() { const [count, setCount] = useState(0); useEffect(() => { const id = setInterval(() => setCount(c => c + 1), 1000); return () => clearInterval(id); }, []); return <div>{count}</div>; } // Repeated in multiple components without abstraction
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Duplicated logic in components | Multiple effect setups | Multiple reflows per component | Higher paint cost due to repeated updates | [X] Bad |
| Custom hook with stable dependencies | Single effect setup reused | Single reflow per update | Lower paint cost with fewer updates | [OK] Good |