Memoization Strategies
On this page
Memoization Is a Tool, Not a Default
- Memoization prevents rerenders caused by reference changes.
- It does not fix state scoping problems.
- Use only when profiling shows measurable benefit.
React.memo
- Prevents rerender when props are shallow equal.
- Useful for leaf components in large lists.
- Do not wrap everything blindly.
useMemo
- Caches expensive computation results.
- Dependency array must be accurate.
- Do not memoize cheap calculations.
useCallback
- Stabilizes function identity.
- Important when passing callbacks to memoized children.
- Dependency correctness is critical.
Example: Correct Boundary
const Row = React.memo(function Row({ item }: { item: { id: string; name: string } }) {
return <div>{item.name}</div>;
});
function List({ items }: { items: { id: string; name: string }[] }) {
return (
<>
{items.map((item) => (
<Row key={item.id} item={item} />
))}
</>
);
}
Common Misuse
- Memoizing everything increases mental overhead.
- Incorrect dependency arrays cause stale closures.
- Memo around unstable objects provides no benefit.
Operational Checklist
- Profiling proves rerender cost is meaningful.
- State placement is already correct.
- Dependency arrays are minimal and correct.
- Memo boundaries are placed near expensive subtrees.