memo, useMemo, and useCallback
Manual memoization is useful when it solves a measured problem. React Compiler now handles many ordinary memoization opportunities automatically, so the default strategy is increasingly correct code β measure β targeted optimization.
The three toolsβ
const Row = memo(function Row({ item }) {
return <div>{item.name}</div>;
});
const visibleItems = useMemo(
() => filterItems(items, query),
[items, query],
);
const handleSelect = useCallback(id => {
setSelectedId(id);
}, []);
All three are about reuse, not durable state.
memo mental modelβ
A memoized component can still render when its own state changes, Context changes, an external-store snapshot changes, or React retries work.
Prop identity can defeat manual memoizationβ
<Row options={{ compact: true }} onSelect={() => select(id)} />
The object and callback are recreated during each parent render.
Before adding more memoization, improve the component API where possible:
<UserCard user={user} />
// or
<UserCard name={name} avatar={avatar} />
useMemo is a performance optimizationβ
Trivial calculations usually do not need it:
const total = price * quantity;
A verified expensive calculation might:
const searchIndex = useMemo(() => {
return buildSearchIndex(largeDataset);
}, [largeDataset]);
The benefit depends on actual cost, update frequency, and whether the cached result gets reused.
Do not use useMemo as durable storage for resources whose lifetime must be guaranteed. Use state, refs, or an appropriate external resource model.
useCallback caches identity, not executionβ
const handleSave = useCallback(() => {
save(record);
}, [record]);
save still runs every time handleSave() is called. React merely may return the same function object while record is unchanged.
Function identity can matter for a memoized child, Hook dependency, subscription API, or external system that stores callbacks.
Functional updates can reduce dependenciesβ
const addTodo = useCallback(text => {
setTodos(current => [
...current,
{ id: crypto.randomUUID(), text },
]);
}, []);
This avoids capturing the current todos snapshot.
Never remove real dependencies just to make a callback βstable.β That creates stale closures.
Restructure Effect dependencies before memoizing themβ
Instead of creating an object on every render and then memoizing it solely for an Effect:
useEffect(() => {
const options = { roomId, serverUrl };
connect(options);
}, [roomId, serverUrl]);
Often the best optimization is eliminating the unnecessary identity dependency.
React Compiler changes the defaultβ
Current React guidance says the Compiler can automatically memoize many values, functions, and component renders. New code generally does not need mechanical memo, useMemo, and useCallback everywhere.
Existing manual memoization should not be mass-deleted without testing because it can affect identity contracts and Compiler output.
Custom comparators are not freeβ
const Chart = memo(ChartImpl, (prev, next) => {
return prev.points === next.points && prev.scale === next.scale;
});
The comparison costs time. Deep equality can cost more than rendering, and ignoring a changed callback can preserve stale behavior.
Memoization chains are architectural warning signsβ
State placement often beats memoizationβ
If hover state lives at the page root, each hover starts an update at the page root. If only a ProductCard needs it, keep it there.
Context bypasses prop memoizationβ
A memoized component that reads Context still renders when that Context value changes. memo compares props; it does not freeze subscriptions.
Huge lists need bigger toolsβ
Per-row memoization may help, but virtualization, pagination, server filtering, and reducing DOM size can provide much larger gains.
Do not call Hooks inside loops. Extract a row component first, then measure whether memoization is needed.
Decision guideβ
Common mistakesβ
- Memoizing trivial calculations.
- Memoizing values whose dependencies change every render.
- Using
useCallbackfor every local handler. - Writing expensive deep custom comparators.
- Treating
useMemoas correctness or durable state. - Optimizing before profiling.
Interview questionsβ
What does useCallback cache?β
The function identity, not the result of calling the function.
Can a memo component still re-render?β
Yes. State, Context, external stores, retries, and other React work can require rendering.
Should every expensive-looking calculation use useMemo?β
No. Measure real cost and reuse first.
How does React Compiler change the default strategy?β
Compiler handles many ordinary memoization opportunities automatically. Manual APIs remain useful for targeted performance or identity control.