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

Combining memo with useCallback and useMemo for Full Effect

See how memo requires stable props—often achieved by wrapping callbacks and objects upstream.

Combining memo with useCallback and useMemo for Full Effect

What you'll learn: How to make React.memo actually work by ensuring props stay stable across parent re-renders.

The Missing Piece

You've learned that React.memo performs shallow comparison on props to decide whether to skip a render. But here's the catch: if you pass callbacks or objects that get recreated on every parent render, React.memo will never skip—because those references change every time.

Think of it like a security guard checking ID cards. memo is the guard, but if you show up with a brand-new ID card every visit (new function/object reference), the guard has to let you through for a "new entry" every single time.

The Three-Part Solution

1. Wrap the child with React.memo to enable bailout checks.

2. Wrap callbacks with useCallback in the parent so the function reference stays stable.

3. Wrap objects with useMemo in the parent so the object reference stays stable.

const ExpensiveChild = React.memo(({ onClick, config }) => {
  // Heavy render work here
});

function Parent() {
  // Stable callback reference
  const handleClick = useCallback(() => {
    console.log('clicked');
  }, []);

  // Stable object reference
  const config = useMemo(() => ({ theme: 'dark' }), []);

  return <ExpensiveChild onClick={handleClick} config={config} />;
}

Without useCallback and useMemo, new references are created on every Parent render, defeating memo's purpose.

When This Matters

This combination shines when:

  • The child renders slowly
  • The parent re-renders frequently
  • Props would otherwise be unstable references

Key Takeaway: React.memo only prevents re-renders when props don't change—use useCallback and useMemo upstream to keep function and object props stable, completing the optimization chain.