Component Composition Over memo
What you'll learn: How using React's built-in composition patterns can prevent unnecessary re-renders without the complexity and overhead of React.memo.
The Core Idea
Instead of wrapping components in React.memo and carefully managing useCallback dependencies, you can often achieve the same performance wins by composing components differently. This means structuring your component tree so that parts that don't need to re-render are naturally isolated from parts that do.
The key patterns are:
- Children prop: Pass JSX as
childrento a parent component - Render props: Pass a function that returns JSX
- Slots: Pass multiple pieces of JSX as different props
Why Composition Wins
When you pass JSX elements as props (like children), React creates those elements in the grandparent component's render. If only the immediate parent re-renders, the already-created children elements aren't recreated—they're just reused. No memoization needed.
Analogy: Imagine handing someone a sealed box (JSX element). When they move to a different room (parent re-renders), they carry the same box—they don't rebuild its contents.
The Pattern in Practice
Instead of a parent creating child components directly (which recreates them on every render), have the parent accept pre-created children:
// Before: Child recreated every parent render
<Parent>
{/* Child created here, in Parent's render */}
</Parent>
// After: Child created in grandparent, passed through
<Parent>
<Child /> {/* Created once in grandparent */}
</Parent>
The Parent just receives children and renders them—it doesn't create them, so Parent's re-renders don't affect Child.
Key Takeaway: Composition patterns leverage React's natural element reuse to prevent re-renders without memoization. Pass JSX as props instead of creating it inside frequently re-rendering parents.