When Props Are Primitives: Automatic Wins
What you'll learn: Why React.memo works perfectly when your component receives only primitive values as props.
Remember how React.memo uses shallow equality to compare props? Here's where that becomes incredibly easy: when your props are primitives (strings, numbers, booleans, null, or undefined), shallow comparison is all you need.
Why Primitives Are Perfect for React.memo
Primitives in JavaScript are compared by value, not by reference. This means:
const a = 5;
const b = 5;
console.log(a === b); // true
const str1 = "hello";
const str2 = "hello";
console.log(str1 === str2); // true
When React.memo checks if props have changed, it uses Object.is() (which works like === for most cases). If your component only receives primitives, this check is bulletproof—no gotchas, no surprises.
Real-World Example
const UserName = React.memo(({ name, age, isActive }) => {
console.log('UserName rendered');
return <div>{name}, {age}, {isActive ? 'Active' : 'Inactive'}</div>;
});
// Parent re-renders with same values:
<UserName name="Alice" age={30} isActive={true} />
// UserName does NOT re-render — all props match perfectly!
As long as name stays "Alice", age stays 30, and isActive stays true, React.memo will skip the re-render. No configuration needed, no useMemo, no headaches.
The Analogy
Think of it like checking if two numbers match. You don't need to compare their "memory address"—5 is always 5. Primitives are their own value.
Key Takeaway: When your component's props are all primitives (string, number, boolean, null, undefined), React.memo works automatically without any extra effort—shallow comparison is sufficient and reliable.