Shallow Equality Explained: Object.is on Props
You'll learn why React.memo doesn't catch changes inside objects or arrays—only changes to the prop references themselves.
How React.memo Actually Compares Props
When you wrap a component with React.memo, React doesn't magically understand what's "inside" your props. It performs a shallow comparison using Object.is() on each prop individually.
Here's what that means:
const MyComponent = React.memo(({ user, items }) => {
// Will only re-render if the REFERENCE to user or items changes
return <div>{user.name} has {items.length} items</div>;
});
What Object.is Checks
Object.is() checks if two values are the same reference (for objects/arrays) or the same value (for primitives):
- Primitives (numbers, strings, booleans): compared by value
- Objects and arrays: compared by reference, not contents
The Critical Limitation
If you pass an object or array prop, React.memo only checks if the reference changed. It doesn't look inside:
const user = { name: "Alice", age: 25 };
// Even if you mutate the object:
user.age = 26;
// React.memo won't detect this change because the reference
// is still the same object in memory
The Analogy
Think of it like checking if someone handed you a different notebook (reference check), not whether they wrote new pages inside the same notebook (deep comparison). React.memo only notices when you get a completely different notebook.
Key Takeaway: React.memo uses Object.is() for shallow comparison—it detects new object/array references, but not changes to their contents. Mutating props or passing new objects created inline will bypass memoization.