Sub topic 4
Skipping re-renders with React.memo
This chapter assumes you know function components, JSX, useState, and that JavaScript objects, arrays and functions are compared by reference. It builds on the earlier chapters about why components re-render and how Object.is compares values.
The problem memo solves
When a component's state changes, React re-renders it and, by default, every child it returns. Children re-render even when their props are identical to the last render. For most components this is cheap. For a large list or a heavy subtree, it adds up.
React.memo wraps a component so React can skip re-rendering it when its props have not changed.
How memo decides
Before re-rendering a memoized component, React compares each new prop with the previous one using Object.is. This is a shallow comparison. If every prop is the same, React reuses the previous result and does not call the component function.
Three details matter:
- Primitives (strings, numbers, booleans) are equal when their values match.
- Objects, arrays and functions are equal only when they are the same reference.
- The comparison applies only to props. State changes inside the memoized component itself still cause a re-render.
A worked example
Row below is wrapped in memo. List holds a selectedId state and passes a stable onSelect function to each row.
import { memo, useCallback, useState } from 'react';
const Row = memo(function Row({ item, onSelect }) {
console.log('render row', item.id);
return <li onClick={() => onSelect(item.id)}>{item.name}</li>;
});
export function List({ items }) {
const [selectedId, setSelectedId] = useState(null);
const handleSelect = useCallback((id) => {
setSelectedId(id);
}, []);
return (
<>
<p>Selected: {selectedId ?? 'none'}</p>
<ul>
{items.map((item) => (
<Row key={item.id} item={item} onSelect={handleSelect} />
))}
</ul>
</>
);
}Clicking a row calls setSelectedId, so List re-renders. Each Row receives the same item object (from the items prop) and the same handleSelect function, so React skips them. The console stays quiet after the first render.
Without memo, every click would log one line per row. Without useCallback, handleSelect would be a new function each render, and memo would never skip anything. Chapters on unstable props and useCallback cover that in detail.
Common errors
- Memoizing a component whose props always change. If a parent passes a new object, array or function on every render, the comparison always fails. You pay for the comparison and still re-render.
- Expecting memo to block state or context updates. A memoized component still re-renders when its own state changes or when a context it reads changes.
- Passing
childrenas JSX. Elements like<Row>text</Row>create a newchildrenvalue every parent render, which defeats memo on that component. - Wrapping everything by habit. Each memoized component adds a comparison on every parent render. Profile first, then memoize components that render often with unchanged props or that are expensive to render.
Checking your understanding
Exercise
A memoized Row receives item (an object from a stable array) and onSelect={(id) => setSelectedId(id)} written inline in the parent. The parent re-renders when selectedId changes. Does Row skip re-rendering? Why, and what fixes it?
Show answer
No. The inline arrow function is a new function on every parent render, so Object.is reports onSelect as changed and Row re-renders. Wrap the handler in useCallback with an empty dependency array (state setters are stable), or pass setSelectedId directly, so the reference stays the same between renders.
Exercise
A memoized component has a useState counter inside it. The parent re-renders with identical props. Does the counter reset or the component re-render? What happens when the counter's own setter is called?
Show answer
With identical props, React skips the component, so it does not re-render and the counter keeps its value. When the component calls its own setter, it re-renders anyway, because memo only compares props from the parent and does not block the component's own state updates.