React.memo and component memoization
How React.memo skips a re-render when props haven't shallowly changed, and the new-object-every-render gotcha that quietly defeats it.
React.memo and component memoization
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- React.memo wraps a component so React skips re-rendering it when its props are shallowly equal to last time, even if its parent re-rendered.
- Shallow equality compares each prop with ===. Primitives like strings and numbers compare by value; objects, arrays, and functions compare by reference, so two props with identical contents still count as different if they're different objects.
- An object, array, or function literal written inline in JSX is created fresh on every render, which means a memoized child receiving one of these as a prop re-renders every time anyway, defeating the memo check silently and with no warning.
- The fix is exactly the two hooks from the previous lesson: useMemo for an object or array prop, useCallback for a function prop, so the reference stays stable across renders when the underlying value hasn't changed.
- React.memo only compares props. It does nothing about a component's own state changing or a context value it consumes changing; those still re-render it normally.
- Wrapping every component in React.memo by default adds a comparison cost to every render for a payoff that rarely materializes; it earns its place on components that are expensive to render and whose parent re-renders often for unrelated reasons.
Skipping a render with a shallow check
Two lessons ago, you saw that a parent re-rendering by default re-renders every child beneath it, whether or not that child’s props actually changed. React.memo is the tool that breaks that default for a specific component.
const Avatar = React.memo(function Avatar({ url, name }) {
console.log("Avatar rendered");
return <img src={url} alt={name} />;
});
function ProfileHeader({ user }) {
const [tab, setTab] = useState("posts");
return (
<div>
<nav>
<button onClick={() => setTab("posts")}>Posts</button>
<button onClick={() => setTab("about")}>About</button>
</nav>
<Avatar url={user.avatarUrl} name={user.name} />
</div>
);
}
Clicking either button updates tab, re-renders ProfileHeader, and without React.memo would re-render Avatar too, even though neither url nor name changed. Wrapped in React.memo, React instead compares this render’s props (url, name) against last render’s props with ===. Since user.avatarUrl and user.name are unchanged strings, every comparison passes, and React skips calling Avatar again entirely, reusing its previous output.
That comparison is shallow: each prop is compared individually by reference, one level deep. Strings and numbers compare by value, so "a" === "a" is simply true. Objects, arrays, and functions compare by reference — two objects with identical contents are still different objects unless they are literally the same one in memory.
The gotcha: a new reference every render
That distinction is where React.memo quietly stops working, and it’s the single most common way people conclude the hook “doesn’t do anything.”
function ProfileHeader({ user }) {
const [tab, setTab] = useState("posts");
return (
<div>
{/* ...nav... */}
<Avatar
url={user.avatarUrl}
name={user.name}
style={{ border: "2px solid gray" }}
/>
</div>
);
}
Add that inline style object and Avatar re-renders on every single click again, even wrapped in React.memo. The object literal { border: "2px solid gray" } is created fresh every time ProfileHeader’s function runs. It has the same contents every time, but it is never the same object, so === returns false, the shallow comparison fails on that one prop, and React re-renders Avatar exactly as if memo weren’t there at all. The same failure happens with an inline array (items={[1, 2, 3]}) or an inline function (onClick={() => doThing()}) passed as a prop — both are new references every render.
This isn’t a bug in React.memo; it’s doing exactly what it’s supposed to do with the information it has. The object genuinely is different from React’s point of view, because reference equality is the only check that’s cheap enough to run on every render without defeating the purpose of memoizing in the first place. A deep comparison would cost more than the render it’s trying to avoid.
The fix is the previous lesson
This is exactly the problem useMemo and useCallback solve, which is why they’re taught immediately before this lesson rather than after it.
function ProfileHeader({ user }) {
const [tab, setTab] = useState("posts");
const avatarStyle = useMemo(() => ({ border: "2px solid gray" }), []);
const handleAvatarClick = useCallback(() => {
console.log("avatar clicked");
}, []);
return (
<div>
{/* ...nav... */}
<Avatar
url={user.avatarUrl}
name={user.name}
style={avatarStyle}
onClick={handleAvatarClick}
/>
</div>
);
}
Now avatarStyle and handleAvatarClick are the same object and function reference across renders of ProfileHeader, as long as their own dependencies (here, none) haven’t changed. Avatar’s shallow comparison sees identical references for every prop and skips the re-render, restoring the benefit React.memo was supposed to provide.
This connects the two previous lessons into one practical rule: React.memo is the component-level gate, and useMemo/useCallback are how you keep anything non-primitive from walking straight through that gate by accident. Used alone, React.memo only reliably helps components that receive exclusively primitive props; reach for the other two hooks the moment an object, array, or function prop enters the picture.
As with the previous lesson, this isn’t a default to apply everywhere. React.memo adds its own comparison cost to every render of the wrapped component, so it pays off on components that are genuinely expensive to render and that sit beneath a parent that re-renders often for reasons unrelated to that component’s own props — a large list item, a chart, anything doing real computation in its render body.
What the video covers
Theo’s video explains the mechanism — shallow prop comparison, skipping a re-render — but spends most of its time on exactly the gotcha this lesson centers on: new object, array, and function literals silently defeating memo. It’s deliberately opinionated, arguing that reaching for React.memo by default is often the wrong instinct, which is a useful counterweight once you understand the mechanics from the sections above rather than a neutral first introduction to the API.
Where this fits
With manual memoization covered end to end, the next lesson looks at the tool that’s increasingly doing this work automatically: the React Compiler and automatic memoization.
Check yourself
What does React.memo compare to decide whether to skip a re-render?
It shallowly compares every prop passed to the component this render against the props from the last render, using ===. If every prop is equal, React reuses the previous render output instead of calling the component function again.
Why does passing an inline object literal as a prop defeat React.memo even when its contents never change?
An object or array literal written directly in JSX, such as style={{ color: 'red' }}, creates a brand-new object every time the parent renders. Shallow equality compares objects by reference, not contents, so that new object is never === to the previous one, and the memoized child re-renders every time regardless of what's inside it.
How do useMemo and useCallback fix the prop-identity problem that breaks React.memo?
They cache the object, array, or function across renders and only produce a new reference when something in their dependency array actually changes. Passing that cached value as a prop means the memoized child sees the same reference on renders where nothing relevant changed, so the shallow comparison succeeds and the re-render is skipped.
Does wrapping a component in React.memo stop it from re-rendering when its own state changes?
No. React.memo only affects re-renders triggered by a parent passing new props. A component's own state changing, or a context value it reads changing, still causes it to re-render normally, because those triggers have nothing to do with the props comparison memo performs.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.