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

Composition Alternatives to memo + useCallback

Using children prop, component composition, or lifting content up to avoid needing memoization entirely.

Composition Alternatives to memo + useCallback

What you'll learn: How to restructure your components to avoid needing memoization in the first place.

The Better Fix: Don't Need Memoization at All

You've learned the memo + useCallback pattern to prevent unnecessary re-renders. But there's often a simpler solution: restructure your components so re-renders don't cascade down in the first place.

React only re-renders children when their parent re-renders. If you can isolate the changing state from the static children, you avoid the problem entirely.

Three Composition Patterns

1. Children Prop

Instead of defining child components inside a parent that has changing state, pass them as children:

// ❌ Bad: List re-renders when count changes
function Parent() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>{count}</button>
      <ExpensiveList /> {/* Re-renders unnecessarily */}
    </div>
  );
}
// ✅ Good: List is defined outside, stays stable
function Parent({ children }) {
  const [count, setCount] = useState(0);
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>{count}</button>
      {children} {/* Doesn't re-render */}
    </div>
  );
}

<Parent><ExpensiveList /></Parent>

2. Lifting Content Up

Move the stateful logic into a smaller wrapper component, leaving heavy components outside its scope.

3. Splitting Components

Extract the changing state into its own small component, isolating re-renders to just that piece.

The Mental Model

Think of components like nested boxes. If the outer box shakes (re-renders), everything inside shakes too. But if you hand the inner boxes to the outer box from outside (via props/children), they stay still.

Key Takeaway: Before reaching for memo and useCallback, ask: "Can I restructure this so the expensive component isn't a child of the changing state?" Composition often eliminates the need for memoization entirely.