Performance: useMemo hook
MEDIUM IMPACT
This affects the rendering speed and responsiveness by avoiding unnecessary recalculations during component renders.
import { useMemo } from 'react'; function Component({items}) { const total = useMemo(() => items.reduce((sum, item) => sum + item.value, 0), [items]); return <div>Total: {total}</div>; }
function Component({items}) { const total = items.reduce((sum, item) => sum + item.value, 0); return <div>Total: {total}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Without useMemo | No change | Multiple reflows if calculation triggers state changes | Higher paint cost due to slower JS | [X] Bad |
| With useMemo | No change | Single reflow if dependencies change | Lower paint cost due to cached results | [OK] Good |