React Reference ยท Concepts and Pitfalls
Memoization guide
A practical guide to React memoization: when memo, useMemo and useCallback help, when they are wasted effort, and how the React Compiler changes the picture.
Memoization means remembering the result of work and reusing it when the inputs are the same. React offers three tools: memo skips re-rendering a component when its props are equal, useMemo caches a calculated value between renders, and useCallback caches a function. All three compare dependencies or props with Object.is.
React is fast by default, and each memoization has its own cost: the comparison itself, memory, and code that is harder to read. Add it where it removes noticeable work, not everywhere.
const Row = memo(function Row({ item, onSelect }) { /* ... */ }); const sorted = useMemo(() => sortItems(items), [items]); const handleSelect = useCallback((id) => select(id), []);
Try it
After the first render, each click on "Re-render" logs only Row rendered: unstable. The stable row bails out because both props are unchanged. Wrapping Row in memo without stabilizing onPick would have done nothing.
When memoization is worth it
- A memoized component with an expensive render that often receives the same props. The saving is a whole subtree.
- A calculation that is measurably slow (about a millisecond or more when you profile it) and runs on every render with unchanged inputs.
- A value used as a dependency of another hook or as a prop of a
memochild, where a new identity every render would trigger an effect or a re-render. This is the most common good reason foruseCallbackanduseMemo.
When it is not
- Cheap calculations (string concatenation, filtering ten items). The cache check costs about as much as the work.
- Components that always receive a new object, array or function prop. The comparison never succeeds.
- Props that change on nearly every render. You pay for the comparison and still render.
- "Just in case" wrapping of every function. It adds noise and does not make slow code fast.
Cheaper fixes first
Before reaching for memoization, try restructuring:
- Move state down into the component that uses it, so unrelated siblings are not in the re-render path.
- Lift content up and pass it as
children: elements created by a parent that did not re-render keep their identity, so React skips them. - Avoid unnecessary state: compute values during render from existing state (see Derived state).
- Virtualize long lists so only visible rows render.
- Use a transition for heavy updates so typing stays responsive (see Concurrent rendering).
Usage notes
- Memoization is a performance hint, not a guarantee. React may discard cached values (for example to free memory or while offscreen); your code must still be correct if the calculation runs again.
- Dependencies must be honest. Leaving a value out of the array gives stale results; the lint rule catches this (see ESLint plugin).
memocompares props shallowly withObject.isunless you provide a custom comparison, which is rarely worth the maintenance.- Context bypasses
memo. A memoized component that reads a context still re-renders when the context value changes (see Context performance). - Strict Mode calls your calculation twice in development and reuses the first result, so a counter like the one above would count extra if you wrapped the app in
<StrictMode>. - The React Compiler adds this kind of memoization automatically at build time, including conditional cases hooks cannot express. With it enabled you can usually remove manual
useMemo,useCallbackandmemo, keeping them only where you need a guaranteed stable identity (see React Compiler). - Measure with the Profiler before and after; if the numbers do not move, remove the memoization.