Course contentsShow
← React
Lesson 947 of 2,39624. React.memoFree lesson

The Parent-Child memo + useCallback Pattern

Standard pattern: memo on expensive children, useCallback on parent handlers, measuring the actual benefit.

The Parent-Child memo + useCallback Pattern

What You'll Learn

You'll learn the standard optimization pattern where you combine React.memo on expensive child components with useCallback on parent event handlers, and how to measure if it's actually helping.

The Complete Pattern

When you have an expensive child component that receives callback props from its parent, you need both pieces working together:

  1. Wrap the child component with React.memo
  2. Wrap the parent's callback functions with useCallback

Think of it like a security checkpoint: memo is the guard checking IDs (props), but if you hand the guard a brand-new ID every time (new function reference), they'll never recognize you and let you through.

How It Works

// Parent component
function Dashboard() {
  const [count, setCount] = useState(0);
  
  // Without useCallback, this creates a new function every render
  const handleItemClick = useCallback((id) => {
    console.log('Clicked:', id);
  }, []); // Stable reference
  
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Unrelated: {count}</button>
      <ExpensiveList onItemClick={handleItemClick} />
    </div>
  );
}

// Child component
const ExpensiveList = React.memo(({ onItemClick }) => {
  console.log('ExpensiveList rendered');
  // ... heavy rendering logic
});

Without useCallback, clicking the unrelated button would create a new handleItemClick function, break memo's prop comparison, and re-render ExpensiveList unnecessarily.

Measure the Benefit

Always profile before and after. Open React DevTools Profiler, record interactions, and check:

  • Did the memoized component actually skip renders?
  • Was the render expensive enough to justify the optimization cost?

Sometimes the shallow comparison check itself costs more than just re-rendering.

Key Takeaway: The parent-child memo pattern requires useCallback on callbacks and React.memo on children working together—but only apply it after profiling proves the child's render is actually expensive.