Full answer
Memoization avoids repeating work by remembering a previous result. In React the main tools are memo for components, useMemo for values and useCallback for functions.
Costs:
- Time.
memoruns a shallow comparison of all props on each parent render. For cheap components, comparing can cost about as much as rendering. - Memory. Cached values and old props are kept alive.
- Complexity. Dependency arrays must be complete. A missing dependency gives stale values, and extra dependencies defeat the cache. Code becomes harder to read.
- False confidence. A single unstable prop (an inline object, array or function, or a context value that changes) makes the whole memoization useless, yet the code still looks optimized.
When memoization helps: a component is expensive to render (large lists, charts) and its parent re-renders often with the same props; a value passed to a memoized child or to a dependency array must keep its identity; a calculation is genuinely heavy (measure, for example above a millisecond or so).
When it does not: simple components, props that change on most renders, state that changes on every keystroke in a parent that owns many children (better fix: move that state down), or premature use "just in case".
Alternatives and guidelines. Colocate state, pass children as props so elements keep identity, split components, virtualize long lists, and use useDeferredValue or transitions for heavy updates. The React Compiler memoizes automatically at build time using the rules of React, so manual memoization becomes the exception for cases such as effect dependency control. Measure with the Profiler before and after, and keep a memoization only if it shows an improvement.
The example shows memoized children that still re-render because of an unstable prop, and another that is skipped.
import { memo, useMemo, useState } from 'react';
const Row = memo(function Row({ label, style }) {
console.log(' Row rendered: ' + label);
return <li style={style}>{label}</li>;
});
const STABLE_STYLE = { color: 'green' };
export default function List() {
const [count, setCount] = useState(0);
const memoStyle = useMemo(() => ({ color: 'blue' }), []);
console.log('List rendered, count ' + count);
return (
<div>
<button onClick={() => setCount(count + 1)}>Re-render parent</button>
<ul>
<Row label="inline style object" style={{ color: 'red' }} />
<Row label="constant style" style={STABLE_STYLE} />
<Row label="useMemo style" style={memoStyle} />
</ul>
</div>
);
}The check pressed the button once. The first row re-renders despite memo, because its style object is new every time. The other two are skipped. Output of the run: List rendered, count 0 Row rendered: inline style object Row rendered: constant style Row rendered: useMemo style List rendered, count 1 Row rendered: inline style object
Key points
- Memoization costs comparison time, memory and added complexity.
- One unstable prop (inline object or function) makes memo useless.
- It helps for expensive renders or calculations with stable inputs. Measure first.
- Moving state down and passing children often beat memoization.
- The React Compiler reduces the need for manual memo, useMemo and useCallback.
Common mistakes
- Wrapping components in memo while passing inline objects or functions.
- Adding useMemo to cheap calculations.
- Omitting dependencies and getting stale values.
- Memoizing before profiling.
- Ignoring that moving state down removes the need for memoization.
Follow-ups
- How would you measure whether a memoization is worth keeping?
- How does the React Compiler decide what to memoize?
- Why can a context value defeat memo?
Tips
- Frame memoization as a trade: cost versus skipped work.
- Mention the one-unstable-prop problem.
- Offer moving state down and the Compiler as alternatives.