When Render Props Win: Dynamic Behavior
What you'll learn: When render props become the superior choice over slots and children for injecting content.
The Core Idea
You've seen slots (named JSX props) and the children prop for injecting content. But what if the content you're injecting needs information from the parent to render correctly? Or what if the content must react to changes in parent state? This is where render props shine.
Render props win when the parent must pass data down to the injected content or when the content's behavior depends on the parent's state.
Why Static Content Isn't Enough
When you pass JSX as a prop (header={<div>Title</div>}), that JSX is evaluated immediately where it's written. The parent component receiving it can't inject data into it later — it's already a fixed element.
But with a render prop (header={(data) => <div>{data.title}</div>}), you're passing a function. The parent calls that function with arguments, giving the injected content access to parent state, handlers, or computed values.
When to Choose Render Props
- Parent has data the content needs: A list component passing each item's data to render functions
- Content must respond to parent state: A toggle component letting content access whether it's open/closed
- Content needs parent callbacks: Injected UI needs to trigger parent actions
- Dynamic, not static: The content can't be predetermined at the call site
Think of it like ordering at a restaurant: slots are like ordering "the salmon" (fixed), while render props are like saying "whatever the chef recommends based on what's fresh today" (dynamic).
Key Takeaway: Choose render props when injected content needs runtime data from the parent or must adapt to parent state changes — they enable two-way communication that static JSX props cannot provide.