Combining memo, useMemo, and useCallback
You'll learn how to coordinate all three memoization tools to prevent unnecessary re-renders in parent-child component relationships.
The Complete Pattern
Using React.memo alone on a child component won't help if the parent keeps passing new object or function references as props. The pattern that actually works requires:
- Wrap the child with
React.memo - Wrap callbacks passed to that child with
useCallback - Wrap object/array values passed to that child with
useMemo
Think of it like a chain: memo is a guard at the door checking if props changed. But if you hand it a new callback every render (even if functionally identical), the guard sees "different prop" and lets the re-render through.
The Synergy
const ExpensiveChild = React.memo(({ onAction, config }) => {
// Only re-renders if onAction or config references change
return <div>...</div>;
});
function Parent() {
const [count, setCount] = useState(0);
// Stable callback reference
const handleAction = useCallback(() => {
console.log('Action');
}, []);
// Stable object reference
const config = useMemo(() => ({
theme: 'dark',
size: 'large'
}), []);
// Parent re-renders don't cascade to ExpensiveChild
return <ExpensiveChild onAction={handleAction} config={config} />;
}
Without useCallback and useMemo, the memo wrapper is useless—every parent render creates new function/object references, failing the shallow equality check.
When This Matters
This pattern is overkill for simple components. Use it when profiling shows:
- Expensive child components rendering wastefully
- Parent updates frequently but child props rarely change
- The child's render time justifies the memoization overhead
Key Takeaway: React.memo only prevents re-renders if all props pass shallow equality—combine it with useCallback and useMemo to maintain stable references for callbacks and objects.