Performance: Custom hook best practices
MEDIUM IMPACT
Custom hooks affect interaction responsiveness and rendering efficiency by managing state and side effects cleanly.
function useData() { const [data, setData] = React.useState(null); React.useEffect(() => { fetch('/api/data').then(res => res.json()).then(setData); }, []); return data; }
function useData() { const [data, setData] = React.useState(null); fetch('/api/data').then(res => res.json()).then(setData); return data; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fetching data in hook body | Minimal DOM changes | Multiple re-renders | Multiple paints | [X] Bad |
| Fetching data inside useEffect | Minimal DOM changes | Single re-render | Single paint | [OK] Good |
| State update in hook body | No DOM changes | Infinite re-renders | Continuous paints | [X] Bad |
| State update inside useEffect | No DOM changes | Single re-render | Single paint | [OK] Good |
| Inline function in hook | No DOM changes | Extra child re-renders | Extra paints | [X] Bad |
| Memoized function with useCallback | No DOM changes | Minimal re-renders | Minimal paints | [OK] Good |