Performance: Reusing logic with hooks
MEDIUM IMPACT
This affects interaction responsiveness and rendering efficiency by reducing unnecessary component updates and re-renders.
function useCounter() { const [count, setCount] = React.useState(0); const increment = React.useCallback(() => setCount(c => c + 1), []); return { count, increment }; } function ComponentA() { const { count, increment } = useCounter(); return <button onClick={increment}>Count: {count}</button>; } function ComponentB() { const { count, increment } = useCounter(); return <button onClick={increment}>Count: {count}</button>; }
function ComponentA() { const [count, setCount] = React.useState(0); const increment = () => setCount(count + 1); return <button onClick={increment}>Count: {count}</button>; } function ComponentB() { const [count, setCount] = React.useState(0); const increment = () => setCount(count + 1); return <button onClick={increment}>Count: {count}</button>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Duplicated state logic in components | Multiple independent states | Multiple reflows per component update | Higher paint cost due to repeated renders | [X] Bad |
| Custom hook with memoized callbacks | Single state logic reused | Single reflow per state update | Lower paint cost due to fewer renders | [OK] Good |