Hint
Shallow prop comparison — skips re-render if props are shallowly equal. Useless without stable prop references.
React.memo wraps a component and shallowly compares old and new props. Skips the re-render if props haven't changed.
const ExpensiveList = React.memo(function ExpensiveList({ items, onSelect }) {
// Only re-renders if items or onSelect reference changes
return items.map(item => );
});
// ─── For memo to work, props must be stable references ────────────────────
function Parent() {
const [count, setCount] = useState(0);
// ❌ New array reference every render → memo useless
const items = [{ id: 1 }, { id: 2 }];
const onSelect = (id) => console.log(id);
// ✅ Stable references
const items = useMemo(() => [{ id: 1 }, { id: 2 }], []);
const onSelect = useCallback((id) => console.log(id), []);
return ;
}
Custom comparison:
const List = React.memo(Component, (prev, next) => {
return prev.items.length === next.items.length; // custom equality
});