You type in the search box, and each letter only appears almost 300 ms later. Clicking +1 cart feels heavy too. The cause is the same: the list of 5,000 products below keeps re-rendering, even though its content often hasn't changed.
React performance means getting React to do only the work that's needed: not re-rendering components whose result is the same, and not letting heavy work hold up what the user types. Picture changing one word on page 1 of a book, then the printer reprints all 200 pages. memo is a printer that checks first: "is this page the same as last time? Skip it." But the check also takes time, so it's only worth it for pages that are expensive to print.
When a component re-renders
Rendering means React calls your component function to work out what to show, then changes only the parts of the DOM that differ. A component re-renders when:
- its own state changes (
useState,useReducer), - its parent re-renders: every child renders too, even with exactly the same props,
- a context it reads changes.
"Props changed" isn't a trigger of its own: props can only change because the parent re-rendered. A re-render is usually cheap (1 or 2 ms). It becomes a problem when the component is expensive, a list of 5,000 products for example, and it renders often.
Measure first with the React DevTools Profiler
Don't guess which component is slow. Install the React DevTools extension, open the Profiler tab, press Record, do the interaction that feels slow, then Stop. The flamegraph shows every component that rendered and for how long; grey ones didn't render. In its settings, turn on "Record why each component rendered" so every render comes with its reason.
Here is the result from the Lab on this page: a shop with a search box, a +1 cart button, and a ProductList of 5,000 products. One list render takes about 280 ms (simulated in the Lab, it doesn't really burn the CPU).
| Interaction | App renders | ProductList renders | ProductList props changed? |
|---|---|---|---|
| Type "k" | 1 | 1 | yes, query "" became "k" |
| Type "o" | 1 | 1 | yes, "k" became "ko" |
| Click +1 cart 8 times | 8 | 8 | no |
| Total | 10 | 10 | 2 out of 10, so 20% |
Eight of those list renders were wasted: 8 × 280 ms = 2,240 ms of work with no different result.
React.memo: skip the render when props are the same
import { memo, useState } from 'react';
const ProductList = memo(function ProductList({ query }) {
const visible = products.filter((p) => p.name.includes(query)); // 5,000 products, expensive
return (
<ul>
{visible.map((p) => <li key={p.id}>{p.name}</li>)}
</ul>
);
});
function App() {
const [query, setQuery] = useState('');
const [cart, setCart] = useState(0);
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<button onClick={() => setCart(cart + 1)}>+1 cart ({cart})</button>
<ProductList query={query} />
</>
);
}
memo(...)wraps the component. Before rendering, React compares each prop with the last render (a shallow comparison,Object.is). All the same? The render is skipped.- Click +1 cart: App re-renders, but
querydidn't change, soProductListis skipped. In the table above, the list now renders 2 times, not 10. key={p.id}: a stable key lets React recognise the same row across renders. An array index as the key makes rows get mixed up when the list is filtered or sorted.
useCallback and useMemo: keep props the same
Before you go on, guess: ProductList is wrapped in memo, but App passes onSelect={() => pick(id)} written straight in the JSX. When you click +1 cart, does ProductList re-render or not?
It still re-renders. () => pick(id) written straight in the JSX is a new function on every render, so memo sees the onSelect prop change. The same goes for an object or array made inside render.
const handleSelect = useCallback((id) => setSelected(id), []); // the same function across renders
const sorted = useMemo(() => [...products].sort(byPrice), [products]); // recomputed when products changes
<ProductList query={query} items={sorted} onSelect={handleSelect} />
useCallback(fn, deps)keeps the same function as long asdepsdon't change.useMemo(() => compute, deps)keeps a computed value: for an expensive calculation, or an object/array passed to amemocomponent.[...products].sort(...): copy first, becausesortchanges the original array.
In short: React.memo for components, useMemo for values, useCallback for functions.
Keep the input responsive: useDeferredValue and useTransition
Without a trick, every keystroke waits for the 280 ms list render to finish, so the delay from typing to the letter appearing is about 290 ms. The good INP limit is 200 ms.
const deferredQuery = useDeferredValue(query);
<ProductList query={deferredQuery} /> // ProductList stays memo
React now renders twice per keystroke. The first render is urgent: the input gets the new letter, deferredQuery still holds the old value, and memo skips ProductList. The second render runs in the background with the new value, and it can be interrupted if you type again.
| Mode in the Lab | List renders per keystroke | Input delay |
|---|---|---|
| Nothing | 1, in the urgent render | about 290 ms |
| useDeferredValue without memo | 2, old value and new | about 290 ms |
| useDeferredValue + memo | 1, in the background | about 12 ms |
When you call the setState yourself, use useTransition: startTransition(() => setFilter(value)) marks that update as not urgent, and isPending can drive a loading indicator.
Other patterns that often help
- State colocation: keep state as close as possible to the components that use it. If
queryis only used by the search panel, keep it there, so typing doesn't re-render all of App. - Lists of hundreds or thousands of rows: use virtualisation (
react-window, TanStack Virtual), so only the visible rows render. The details are in the Virtual Scrolling lesson. - Large components that are rarely opened:
const Chart = lazy(() => import('./Chart'))with<Suspense>, covered in the Code Splitting lesson. - React Compiler: in modern React projects, this compiler (version 1.0 came out in 2025) adds memoization automatically at build time. You rarely need to write
memo,useMemoanduseCallbackyourself, but still measure with the Profiler.
The exercise version: when memo pays off
shouldMemoize returns true only when all three conditions hold: renderCount > 5 (renders often), propsChangedPercent < 30 (props rarely change, so memo can often skip), and computationMs > 10 (each render is expensive).
| renderCount | propsChangedPercent | computationMs | Result |
|---|---|---|---|
| 10 | 20 | 15 | true |
| 3 | 20 | 15 | false, rarely renders |
| 10 | 50 | 15 | false, props change often |
| 10 | 20 | 5 | false, the render is cheap |
identifyUnneededReRenders keeps the components with propsChanged === false: the ones that rendered without their props changing, candidates for React.memo. From Header, UserCard and Footer, the result is Header and Footer.
⚠️ Memo that's wasted, or missing
- Wrapping every component in
memo. Comparing props has a cost too. On cheap components it only makes the code harder. Measure first. - A new function, object or array as a prop of a
memocomponent.memothen never skips. UseuseCallbackoruseMemo. useDeferredValuewithoutmemo. The list still renders in the urgent render, so the input still stutters.- In the exercise: use
>and<, not>=and<=. ArenderCountof 5 is not "more than 5", and 30% is not "less than 30".
TL;DR: A component re-renders when its state changes, its parent re-renders, or its context changes. Measure first with the React DevTools Profiler. React.memo skips the render when props are the same, useCallback keeps a function the same, useMemo keeps a value. A new function, object or array on every render defeats memo. useDeferredValue (or useTransition) plus memo keeps the input responsive while a heavy list catches up in the background. Use stable keys, keep state near its users, virtualise long lists, and consider React Compiler. Memo pays off when a component renders often, its props rarely change, and its render is expensive.
⚡ When a component re-renders
Meaning: A component re-renders when its state changes, its parent re-renders, or a context it reads changes. A re-render is usually cheap, but it becomes a problem when the component is expensive and renders often. Measure first with the React DevTools Profiler.
⚡ memo skips a render that's the same
Write:
const ProductList = memo(function ProductList({ query }) {
const visible = products.filter((p) => p.name.includes(query));
return <ul>{visible.map((p) => <li key={p.id}>{p.name}</li>)}</ul>;
});
Guess first: React.memo, useMemo and useCallback sound alike. What's the difference?
React.memo is for components, useMemo for values, useCallback for function references. A new function, object or array on every render means memo never skips.
Meaning: In the Lab, clicking +1 cart no longer re-renders the list of 5,000 products: from 10 renders down to 2.
⚡ Keep the input responsive
Write:
const deferredQuery = useDeferredValue(query);
<ProductList query={deferredQuery} />
Meaning: The input gets its new letter first, and the heavy list catches up in the background. Together with memo, the typing delay drops from about 290 ms to about 12 ms.