Course contentsShow
← React
Lesson 1152 of 2,39629. Composition: SlotsFree lesson

Real-World Pattern Selection Strategy

Build a decision framework: start with children, upgrade to slots for multiple insertions, reach for render props when sharing state or behavior.

Real-World Pattern Selection Strategy

What you'll learn: A practical decision-making framework for choosing between children, slots, and render props in real-world React components.

The Mental Model: Start Simple, Upgrade When Needed

Think of content composition patterns as tools in a toolbox. You don't grab the power drill for a simple nail—start with the hammer. Similarly, always begin with children. It's the simplest, most intuitive pattern for users of your component.

The Decision Tree

Start with children when:

  • You have a single content insertion point
  • The parent component doesn't need to manipulate what's inside
  • Example: <Card>{content}</Card>

Upgrade to slots when:

  • You need multiple, named insertion points
  • Each area serves a distinct purpose
  • Example: A modal needs header, body, and footer

Reach for render props when:

  • The parent must share state or behavior with the rendered content
  • The child needs dynamic data from the parent
  • You're exposing interactive capabilities
  • Example: A data fetcher passing data and loading state

Real-World Example Flow

Building a data table? Start with children for simple tables. Realize you need separate header and body sections? Switch to slots. Need to expose sorting state and callbacks? Now render props make sense.

The key insight: patterns exist on a complexity spectrum. Don't over-engineer from the start, but recognize when complexity justifies the upgrade.

Practical Questions to Ask

  1. How many content areas? (One = children, multiple = slots)
  2. Does inserted content need parent data? (Yes = render props)
  3. Is the API intuitive for your team? (Simpler usually wins)

Key Takeaway: Start with children by default, upgrade to slots when you need multiple named areas, and reach for render props only when you must share state or behavior with the inserted content. Let real requirements drive the pattern, not anticipated complexity.