Slot Props vs Children Prop
What you'll learn: When to use named slot props versus the children prop, and how to strategically combine both for maximum component flexibility.
The Core Distinction
The children prop is React's default slot—it represents content placed between component tags. Slot props are explicitly named props that accept JSX elements. The choice isn't either/or; it's about understanding what each communicates to developers using your component.
Use children when:
- There's one primary content area
- The content represents the "main thing" this component wraps
- The usage feels natural:
<Card>content here</Card>
Use named slot props when:
- You need multiple content areas
- Each area has a distinct purpose (header, footer, sidebar)
- Order doesn't match DOM placement (footer prop might render at top)
- You want explicit, self-documenting APIs
Combining Both Effectively
The most flexible components often use both:
function Dialog({
header,
footer,
children
}: {
header?: React.ReactNode;
footer?: React.ReactNode;
children: React.ReactNode
}) {
return (
<div className="dialog">
{header && <div className="dialog-header">{header}</div>}
<div className="dialog-body">{children}</div>
{footer && <div className="dialog-footer">{footer}</div>}
</div>
);
}
// Usage feels natural
<Dialog
header={<h2>Confirm</h2>}
footer={<button>OK</button>}
>
Are you sure?
</Dialog>
Here, children handles the main content (what the dialog is about), while header and footer handle distinct UI regions. This creates an intuitive API where the most important content stays between tags.
Key Takeaway: Use children for your component's primary content and named slot props for secondary, well-defined regions. Combining both creates APIs that are both flexible and self-documenting.