Course contentsShow
← React
Lesson 2271 of 2,39650. Performance: Optimization TechniquesFree lesson

The Shallow Equality Check

How React.memo compares props and what 'shallow' means for objects and arrays.

The Shallow Equality Check

You'll learn: How React.memo compares props under the hood and what "shallow" means for complex values like objects and arrays.

What Shallow Comparison Means

When you wrap a component with React.memo, React performs a shallow equality check on props between renders. "Shallow" means React only checks if the reference of each prop has changed — it doesn't look inside objects or arrays to compare their contents.

Think of it like checking if two boxes are the same box (by comparing their addresses), not whether they contain the same items inside.

How It Works

For primitive values (strings, numbers, booleans), shallow comparison works perfectly:

// These are equal
prevProps.count === nextProps.count // true if both are 5

But for objects and arrays, React only checks if they're the same reference:

// Different objects, same content — React sees them as DIFFERENT
const obj1 = { name: 'Alice' };
const obj2 = { name: 'Alice' };
obj1 === obj2 // false — different references!

// Same reference — React sees them as IDENTICAL
const obj3 = obj1;
obj3 === obj1 // true

Why This Matters for memo

If you pass a new object or array literal on every render, even with identical contents, React.memo will think the props changed and re-render the component anyway:

// ❌ memo won't help — new object every render
<UserCard user={{ name: 'Alice', age: 30 }} />

// ✅ memo works — same reference if parent didn't re-create it
<UserCard user={stableUserObject} />

Key Takeaway: React.memo's shallow equality check only compares prop references, not their contents — new object/array literals will always fail the comparison, even if their data is identical.