useCallback for Referential Stability
What you'll learn: How useCallback prevents unnecessary child re-renders by keeping callback references stable across parent renders.
The Referential Stability Problem
Every time a component renders, any functions defined inside it are recreated with new references—even if their logic is identical. When you pass these callbacks as props to React.memo-wrapped children, the shallow equality check sees a "different" function every time, defeating memoization entirely.
function Parent() {
// New function reference on every render ❌
const handleClick = () => console.log('clicked');
return <MemoizedChild onClick={handleClick} />;
// Child re-renders every time, even with memo
}
How useCallback Solves This
useCallback returns the same function reference between renders unless its dependencies change:
function Parent() {
const [count, setCount] = useState(0);
// Stable reference across renders ✓
const handleClick = useCallback(() => {
console.log('clicked');
}, []); // Empty deps = never changes
return (
<>
<button onClick={() => setCount(count + 1)}>
Parent count: {count}
</button>
<MemoizedChild onClick={handleClick} />
</>
);
}
Now when Parent re-renders (e.g., count changes), handleClick keeps the same reference, MemoizedChild sees identical props, and skips rendering.
The Combo Pattern
useCallback and React.memo work together: memo checks prop equality, useCallback ensures callbacks stay equal.
Key Takeaway: Use useCallback to stabilize callback references passed to memoized children—without it, React.memo can't prevent re-renders caused by function props. Profile first to confirm the optimization actually helps.