Skip to content

useMemo, useCallback and memo: when they help

Deep diveConcept

You wrapped a slow list in memo, added useCallback for good measure, and the profiler shows the same renders as before. Here is what these three tools actually compare, and how to tell when the comparison costs more than the render it saves.

01 Memo on every row, and nothing changed

A list of four items with a title box above it. Each Row is wrapped in memo, so typing in the title should leave the rows alone. Each component logs when React calls it.

App.jsx
import { useState, memo } from 'react';
const items = [
{ id: 1, label: 'Milk' },
{ id: 2, label: 'Eggs' },
{ id: 3, label: 'Bread' },
{ id: 4, label: 'Tea' },
];
const Row = memo(function Row({ item, selected, onSelect }) {
console.log(`Row ${item.label}`);
return (
<li className={selected ? 'on' : ''} onClick={() => onSelect(item.id)}>
{item.label}
</li>
);

Type one letter in the title box. This is the real console output (React 19.3, no StrictMode):

Text
App
Row Milk
Row Eggs
Row Bread
Row Tea

Every row rendered, exactly as if memo were not there. Nothing is broken. memo did its job: it compared, found a difference, and let the render through.

02 What memo checks before it skips

When a parent renders, React normally calls every child again. For a component wrapped in memo, it first compares the new props with the props from the last render, one prop at a time, with Object.is. If every prop is equal, React reuses the last result and does not call the component. If any prop differs, it renders as usual.

  • Object.is compares numbers, strings and booleans by value: false equals false.
  • It compares objects, arrays and functions by identity: two arrow functions with the same code are still two different objects.
  • It is shallow. React does not look inside item; it only asks whether item is the same object as last time.
memo checks the props of Row Eggs after one keystroke
Step 1 of 5
props of Row Eggs
last renderthis renderObject.is
itemitems[1]items[1]
selectedfalsefalse
onSelectarrow #1arrow #2
what React did
Empty
  • being compared
  • same value
  • different value
  • just done

You typed a, so App rendered and returned a fresh <Row> element for Eggs. Row is wrapped in memo, so before calling it React holds the props from the last render next to the new ones.

The fix is to hand Row the same function on every render. That is what useCallback is for:

JavaScript
const select = useCallback((id) => setSelected(id), []);
// ...
<Row key={item.id} item={item} selected={item.id === selected} onSelect={select} />
The same keystroke after wrapping the handler in useCallback
props of Row Eggs
last renderthis renderObject.is
itemitems[1]items[1]same
selectedfalsefalsesame
onSelectselectselectsame
  • same value

All three props are equal, so React reuses the last result of Row for Eggs and does not call it. The same happens for the other three rows: one keystroke now logs only App.

03 useCallback and useMemo keep a value the same

Neither hook skips a render. Each one stores a value in the component between renders and gives the same value back while its dependency array is unchanged. useMemo(fn, deps) stores what fn returns; useCallback(fn, deps) stores fn itself. The two lines below behave the same:

JavaScript
const select = useCallback((id) => setSelected(id), []);
const select = useMemo(() => (id) => setSelected(id), []);

That gives them exactly two jobs: skip a slow calculation, or keep a value's identity stable so that something downstream that compares (a memo child, or another hook's dependency array) sees "no change". A useCallback whose function goes to a plain, non-memo child does nothing useful: the child renders anyway, because its parent rendered.

Nobody checks it
const onSave = useCallback(save, [draft]);
// Form is not wrapped in memo
return <Form onSave={onSave} />;
memo checks it
const onSave = useCallback(save, [draft]);
// const Form = memo(function Form...)
return <Form onSave={onSave} />;

04 The useMemo slot, render by render

Here is one useMemo that feeds a memo child, followed through four renders. The output is the real console log, with App logging after the hook line and filterItems logging each time it runs.

One useMemo across four renders
Step 1 of 7
useMemo slot in App
Empty
work done
Empty
  • just stored or run
  • reused
  • skipped
  • kept

App keeps title and query in state and filters four items with useMemo(() => filterItems(items, query), [query]). The result goes to List, which is wrapped in memo. Nothing has rendered yet, so the slot is empty.

"" query

Without useMemo, the filter would return a new array on every render, List would see a different rows prop each time, and its memo would never skip. The two tools only pay off as a pair.

05 Count the renders yourself

Do not trust a memo you have not measured. Put a console.log at the top of each component, then do the same three things to each version: type one letter, click Eggs, then click Tea. These are the calls to Row from real runs of the four versions:

Calls to Row per action, four rows, React 19.3 in jsdom (real runs)
VersionType a letterClick EggsThen click Tea
Plain `Row`444
`memo(Row)`, inline `onSelect`444
`memo(Row)` + `useCallback`012
Same, but `item={{ ...item }}`444

Read the third row closely. Clicking Eggs renders one row (Eggs, whose selected turned true). Clicking Tea renders two: Eggs turns false and Tea turns true. memo lets through exactly the rows whose props changed. The last row shows how easily that breaks: copying the item inline makes a new object per render, and every row renders again.

  • In the browser, the React DevTools Profiler shows each render with its time. Turn on Record why each component rendered in its settings to see which props changed, such as onSelect.
  • Under StrictMode in development, React calls each component and each useMemo function twice to catch impure code, so counts double. Count in production, or read the Profiler instead.
  • Measure with the production build when you care about time. Development React does extra checks that make every render slower.

06 The trade: a comparison against a render

Memoising is never free. On every render of the parent, memo compares each prop, and useMemo and useCallback compare each dependency and keep the old value in memory. You get something back only when the comparison says "equal" and what it lets you skip costs more than the comparison did.

Props change on every renderCompare, then renderA memo on a component that gets count or Date.now() fails its check every time. You pay for both and save nothing.
A tiny leaf, stable propsSaves almost nothingSkipping a <span>{label}</span> saves one cheap function call. The win is too small to notice, and every wrapper adds code to read.
A large subtree, stable propsThe whole subtree skippedA chart, an editor or a list of hundreds of rows whose props rarely change. One cheap check stands in for many component calls.

So reach for these tools in this order. First measure, and find a render that is actually slow. Then check whether its props are usually equal when the parent renders. Only then wrap it in memo, and give it stable props with useMemo and useCallback. Often a cheaper fix exists: move the state that changes down into the component that uses it, or pass the slow part in as children from a parent that does not render.

A custom compare function, memo(Chart, (a, b) => deepEqual(a.data, b.data)), runs on every parent render. Walking a large data array can cost more than rendering the chart. Worse, a compare that ignores a prop (say a function) can skip a render you needed, and the screen shows stale values.

07 When memo stops holding

<Card> <b>Hi</b> </Card>children are new on every renderJSX makes a new element object each time the parent runs, so a memo component's children prop always differs. A memoised Card with children renders every time (checked in the same runs).
const Badge = memo(function Badge() { const theme = useContext(Theme);Context goes straight through memomemo only compares props. A component that reads a context renders when that context's value changes, whatever its props say.
useMemo(() => total(cart), [cart, { tax }])A dependency that is new each renderAn object, array or arrow in the dependency array is a new value each time, so the cache never hits. Depend on tax itself, the primitive.
const [n, setN] = useState(0);A component's own statememo never blocks a render that the component asked for itself, through its own state setter or a context it reads.
useEffect(sync, [options]); // options from useMemoUsing useMemo for correctnessReact may throw away a cached value, for example in development or when a component suspends. If code breaks without the cache (an effect firing twice, a subscription restarting), fix the code, not the cache.

If your project uses React Compiler (stable since late 2025), it adds this memoisation for you at build time, at a finer grain than you would by hand. You still need to know the rule it follows: a value is only skipped when React can see it is the same.

08 Check yourself

Question

const Price = memo(function Price({ amount }) {...}). Its parent passes amount={cart.total}, a number that changes on every click. What does memo do on each click?

Question

A parent wraps a handler in useCallback and passes it to a child that is not wrapped in memo. When the parent renders, does the child render?

Question

const opts = useMemo(() => ({ sort }), [sort]) goes to a memo(Table). You type in an unrelated box and the parent renders. Does Table render?

09 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?