Category: react
What Is React.memo?
Published on 27 Oct 2026
Explanation
React.memo lets React skip rendering a component when its props are unchanged by shallow comparison. It is an optimization and does not prevent rerenders caused by the component's own state or consumed context.
Code:
const UserCard = React.memo(function UserCard({ name }) {
console.log('UserCard rendered');
return <p>{name}</p>;
});
Explanation
React.memo compares prop references. A new object, array, or function created on each parent render counts as a changed prop even if its contents appear identical.
Code:
function Parent() {
const settings = { theme: 'dark' }; // New object each render
return <Child settings={settings} />;
}
const Child = React.memo(({ settings }) => <p>{settings.theme}</p>);
Explanation
When a memoized child receives a callback, useCallback can keep the callback stable across parent renders, provided its dependencies have not changed.
Code:
const Child = React.memo(({ onClick }) => (
<button onClick={onClick}>Open</button>
));
function Parent() {
const handleClick = useCallback(() => console.log('Open'), []);
return <Child onClick={handleClick} />;
}
Explanation
State placed high in the component tree can cause many descendants to rerender when it changes. Moving state into the smallest component that needs it can reduce unnecessary work.
Code:
function Page() {
return (
<>
<Header />
<SearchBox />
<ExpensiveReport />
</>
);
}
function SearchBox() {
const [query, setQuery] = useState('');
return <input value={query} onChange={(e) => setQuery(e.target.value)} />;
}
Explanation
Not every rerender is expensive or harmful. Use React DevTools Profiler to identify components that render frequently or take significant time before adding memoization.
Code:
const Report = React.memo(function Report({ rows }) {
return rows.map((row) => <p key={row.id}>{row.name}</p>);
});
// Memoization helps only if props are stable and rendering is worth skipping.