useMemo, useCallback and memo: when they help
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.
Type one letter in the title box. This is the real console output (React 19.3, no StrictMode):
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.iscompares numbers, strings and booleans by value:falseequalsfalse.- 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 whetheritemis the same object as last time.
- 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:
- 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:
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.
const onSave = useCallback(save, [draft]);// Form is not wrapped in memoreturn <Form onSave={onSave} />;
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.
- 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.
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:
| Version | Type a letter | Click Eggs | Then click Tea |
|---|---|---|---|
| Plain `Row` | 4 | 4 | 4 |
| `memo(Row)`, inline `onSelect` | 4 | 4 | 4 |
| `memo(Row)` + `useCallback` | 0 | 1 | 2 |
| Same, but `item={{ ...item }}` | 4 | 4 | 4 |
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
StrictModein development, React calls each component and eachuseMemofunction 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.
memo on a component that gets count or Date.now() fails its check every time. You pay for both and save nothing.<span>{label}</span> saves one cheap function call. The win is too small to notice, and every wrapper adds code to read.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.