memo + Stable Props Pattern
What you'll learn: How to combine React.memo with useMemo and useCallback to prevent memoized components from re-rendering unnecessarily.
The Missing Puzzle Piece
You've wrapped a component in React.memo, but it's still re-rendering on every parent update. Why? Because the props you're passing are new references each time—objects, arrays, or functions created inline during render. React.memo performs a shallow equality check, and { x: 1 } !== { x: 1 } in JavaScript.
The solution is the Stable Props Pattern: ensure every prop passed to a memoized component maintains the same reference across renders unless the underlying value truly changes.
How It Works
Use useMemo to stabilize object and array props:
const config = useMemo(() => ({ theme: 'dark', size: 'large' }), []);
Use useCallback to stabilize function props:
const handleClick = useCallback(() => {
console.log('Clicked');
}, []);
Now when you pass config and handleClick to a React.memo component, those references stay constant across renders—triggering re-renders only when dependencies actually change.
The Complete Pattern
const ExpensiveChild = React.memo(({ data, onClick }) => {
// Only re-renders if data or onClick reference changes
});
function Parent() {
const data = useMemo(() => ({ id: 1, name: 'Item' }), []);
const onClick = useCallback(() => alert('Clicked'), []);
return <ExpensiveChild data={data} onClick={onClick} />;
}
Without useMemo/useCallback, the memoization fails because props are "different" every time.
Key Takeaway: React.memo only prevents re-renders if props are referentially stable—combine it with useMemo and useCallback to keep object, array, and function props consistent across renders.