Skip to main content

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.

Modern memoization strategy
1Write correct pure React code
2Measure a real bottleneck
3Let Compiler handle ordinary reuse
4Add manual memoization only for precise needs
5Profile again

The three tools​

memo
Skip a component render when props are equal and nothing else requires it.
useMemo
Reuse a calculated value while dependencies are equal.
useCallback
Reuse a function identity while dependencies are equal.
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​

memo adds a prop-comparison gate
Parent renders
New parent render attempt
Child props
Object.is per prop by default
Skip child render
Only when nothing else requires update

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} />
Why is the child rendering?
Its own state / Context / store changedmemo cannot prevent a required update
Parent rendered and props are effectively unchangedCompiler or targeted memo may help if the child is expensive
Props are recreated unnecessarilyFix prop/ownership design before building memoization chains

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.

Safe migration from manual memoization
1Enable Compiler safely
2Verify correctness
3Profile
4Remove one manual optimization only if justified
5Compare behavior + performance again

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​

Manual optimization can cascade
Child uses memo
Skip depends on stable props
Parent stabilizes callback
useCallback
Parent stabilizes object
useMemo
Revisit ownership / API shape
Architecture may beat the chain

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.

Narrow ownership naturally narrows update scope
Page-owned hover
Large subtree reconsidered
ProductCard-owned hover
Only local owner updates

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​

What kind of optimization is justified?
Expensive pure calculation repeats with same inputsCompiler or targeted useMemo after measurement
Expensive child repeatedly receives equal propsCompiler or targeted memo
Stable callback identity is an external/API contractuseCallback may be appropriate
Large collection dominates DOM/render costVirtualization/data architecture first
Local state causes giant ancestor updatesMove state toward its real owner
Tiny cheap component renders frequentlyUsually leave it alone

Common mistakes​

  • Memoizing trivial calculations.
  • Memoizing values whose dependencies change every render.
  • Using useCallback for every local handler.
  • Writing expensive deep custom comparators.
  • Treating useMemo as 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.

References​