You click the +1 button in a corner of the page, and the 2,000-row table below it renders again too. Yet nothing in the table has changed at all. Why does that happen? Because the table is a child of the component that has the +1 button, and when a parent renders again, all its children render again too. So how do you stop it?
One way is memo. memo wraps a component so React skips re-rendering it as long as its props are the same as in the previous render. Say you're the person who writes the menu board in a café. Every time the manager says "update the board", you first check the menu sheet they hand you. If it's the same sheet as the one before, the board doesn't need rewriting. If it's a different sheet, you rewrite the board from the start, even if the content happens to look the same. In React, you are the component wrapped in memo, and the menu sheet is the props. Rewriting the board is a re-render, and the order "update the board" comes every time the parent renders again.
Re-render: a parent drags its children along
A component re-renders when its state changes: React runs the component function again to find out what it should show now. What people often miss: every child component inside it re-renders too, even when their props haven't changed at all.
In this example, App has a counter, and its child ExpensiveList shows an array items that never changes:
const items = ["A", "B", "C"]
function ExpensiveList({ items }) {
console.log("List rendered")
return (
<ul id="list">
{items.map((item) => <li key={item}>{item}</li>)}
</ul>
)
}
function App() {
const [count, setCount] = useState(0)
return (
<div>
<span id="count">{count}</span>
<button id="inc" onClick={() => setCount((c) => c + 1)}>+1</button>
<ExpensiveList items={items} />
</div>
)
}
Click +1 three times, then count the renders:
| Event | App render no. |
ExpensiveList render no. |
|---|---|---|
| Page opens | 1 | 1 |
| Click +1 | 2 | 2 |
| Click +1 | 3 | 3 |
| Click +1 | 4 | 4 |
items never changes, yet the list re-renders 3 times. So List rendered prints 4 times: once when the page opens, and once on each click. With 3 items you won't notice. With a 2,000-row table or a heavy chart, every click gets slow.
Wrap it in memo
Here is the same ExpensiveList, now wrapped in memo:
const ExpensiveList = memo(function ExpensiveList({ items }) {
console.log("List rendered")
return (
<ul id="list">
{items.map((item) => <li key={item}>{item}</li>)}
</ul>
)
})
memo(...)takes a component and returns a version that remembers its last props. In a real project you import it withimport { memo } from "react". In the exercise editor,memois already available. You may write that import line, but you don't have to.- Every time
Appre-renders, React compares the new props ofExpensiveListwith the old ones. If they are all the same, React skips the render and reuses the old output. - If one prop is different,
ExpensiveListre-renders as usual. console.log("List rendered")inside the component shows whether the render was skipped: that line only appears when the component really renders.
With memo, the result of those three clicks changes: App renders 4 times, but ExpensiveList only once, when the page opens.
Why memo can fail: a new prop every render
memo compares each prop with Object.is, which works much like ===. Numbers and strings are compared by value: 3 === 3 is true. But arrays, objects and functions are compared by identity, not by what's inside. Identity means: are the two values you compare the same thing in memory? It's like the menu sheet in the café: the sheet is the thing in memory, and what's written on it is the content. You check whether it's the same sheet, not what's written on it. Look at these two comparisons of arrays:
["A", "B", "C"] === ["A", "B", "C"] // false: two different arrays
const a = ["A", "B", "C"]
a === a // true: the same array
Now move items inside App:
function App() {
const [count, setCount] = useState(0)
const items = ["A", "B", "C"] // a NEW array on every App render
// ...
return <ExpensiveList items={items} />
}
On every render, the line const items = [...] runs again and makes a new array. The content is the same, but the identity is different. So memo treats the prop as changed, and List rendered prints again on every click. The same happens with items={["A", "B", "C"]} written directly in the JSX: the array is new on every render, so List rendered prints again too. Functions work the same way: a prop like onSelect={() => ...} written in the JSX makes a new function on every render.
What ExpensiveList receives |
List renders after 3 clicks |
|---|---|
No memo |
4 |
memo + items made inside App |
4 |
memo + onSelect={() => ...} written in the JSX |
4 |
memo + items outside App |
1 |
memo + onSelect from useCallback |
1 |
How to keep a prop the same:
- Data that never changes: put it outside the component, like
const items = [...]aboveApp. The array is made only once. - Data calculated from props or state:
useMemo(() => [...], [deps])returns the same array as long as its dependencies don't change. - Functions:
useCallback((item) => ..., [])returns the same function on every render.
When memo is worth it
React is fast, and you won't notice most re-renders. Use memo when both of these are true:
- The component is expensive to render (it takes a lot of work): a long list, a table, a chart.
- Its parent re-renders often with the same props for that component, for example a parent with an input someone is typing into.
memo doesn't stop re-renders caused by the component's own state, or by a change in a context it reads. If your project uses React Compiler, the compiler adds memoization (remembering results, as memo does) automatically at build time.
⚠️ Why does the list still re-render?
- A new array, object or function every render.
memo+itemsmade insideAppis no better than nomemo. Move it outside the component, or useuseMemoanduseCallback. console.logoutside the component. TheList renderedline has to be insideExpensiveList, beforereturn. Outside the component it runs only once, when the code loads, so it proves nothing.- A prop that really changes. A
<Badge count={count} />wrapped inmemostill re-renders every timecountgoes up, because the prop really is different. That's not a bug:memoworks that way. memoon every component. Comparing props has a cost too. Leave small components that are cheap to render withoutmemo.
TL;DR: A parent that re-renders makes all its children re-render too. memo(Component) skips that re-render as long as the props are the same. Props are compared with Object.is (much like ===): numbers and strings by value, arrays, objects and functions by identity (whether it's the same thing in memory). An array or function made inside the parent is new on every render, so memo fails: put it outside the component, or use useMemo and useCallback. The proof is a console.log inside the component: when memo works, that line doesn't appear again when the parent's counter goes up. Use memo only for components that are expensive to render (they take a lot of work).
⚡ A parent drags its children along
Meaning: Every time a parent renders again, all its children render again too, even when their props didn't change. For example, App has a +1 button, and its child, ExpensiveList, shows a list whose content never changes. ExpensiveList prints List rendered each time it renders. Click +1 three times, and List rendered prints 4 times: once when the page opens, and once on each click.
⚡ Wrap it in memo
Write:
const ExpensiveList = memo(function ExpensiveList({ items }) {
console.log("List rendered")
return (
<ul id="list">
{items.map((item) => <li key={item}>{item}</li>)}
</ul>
)
})
Meaning: memo wraps a component. React skips its re-render as long as all its props are the same as in the previous render. Here the prop items (what the list holds) doesn't change, so a click on +1 doesn't print List rendered again.
⚡ A new prop on every render
Write:
function App() {
const [count, setCount] = useState(0)
const items = ["A", "B", "C"] // a NEW array on every App render
// ...
return <ExpensiveList items={items} />
}
Guess first: ExpensiveList is already wrapped in memo, but now items is made inside App. Why does the list still render again on every click?
Every render of App makes a new array. The content is the same, but the identity is different: it's not the same array in memory. memo compares arrays by identity, so it treats the prop as changed. When props change on every render, memo is no use.
Meaning: To keep the prop the same, put data that never changes outside the component. For data calculated from props or state, use useMemo. For functions, use useCallback.