What React.memo Does: Shallow Prop Comparison
You'll learn how React.memo prevents unnecessary re-renders by comparing props shallowly before deciding whether to update a component.
What is React.memo?
React.memo is a higher-order component that wraps your component and adds a performance optimization layer. When a parent component re-renders, React.memo checks if the wrapped component's props have changed. If they haven't, React skips re-rendering that component entirely—it just reuses the last rendered result.
The key word here is shallow. React.memo performs a shallow equality check, meaning it compares each prop using Object.is() (similar to ===). For primitive values like strings, numbers, or booleans, this works perfectly. For objects, arrays, and functions, it only checks if the reference is the same—not whether the contents changed.
Think of it Like a Gatekeeper
Imagine React.memo as a security guard at a component's door. Every time the parent tries to re-render the child, the guard asks: "Are these props exactly the same references as last time?" If yes, the guard says "nothing changed, no need to re-render." If any prop reference differs, the component re-renders.
The Shallow Comparison Caveat
This shallow check is why referential stability matters so much. If you pass a new object or function on every render (even with identical contents), React.memo sees different references and allows the re-render anyway. That's why useMemo and useCallback become important when working with memoized components—they keep those references stable.
Key Takeaway: React.memo wraps a component to skip re-renders when props haven't changed, using a shallow equality check that compares references, not deep values.