Children vs Explicit Props
What you'll learn: When to use children for flexible composition versus passing content through named props for better control and clarity.
The Core Decision
You've seen how children lets you nest content inside component tags. But sometimes it's better to pass that same content through explicit named props instead. The key difference? Flexibility versus specificity.
Children shines when:
- You're building generic wrapper components (layouts, containers)
- The component doesn't need to know what it's wrapping
- You want a natural HTML-like nesting feel
Named props work better when:
- Your component needs multiple distinct content areas
- You want to make the purpose of each piece explicit
- You need better IDE autocomplete and documentation
A Simple Example
// Using children - flexible, simple
function Card({ children }) {
return <div className="card">{children}</div>;
}
<Card><p>Any content here</p></Card>
// Using named props - explicit, controlled
function Card({ header, body, footer }) {
return (
<div className="card">
<div className="card-header">{header}</div>
<div className="card-body">{body}</div>
<div className="card-footer">{footer}</div>
</div>
);
}
<Card
header={<h2>Title</h2>}
body={<p>Content</p>}
footer={<button>Action</button>}
/>
When to Choose Which
Use children for simple wrappers that don't care about structure. Use named props when your component needs to arrange multiple pieces of content in specific places. Think of children as a blank canvas, and named props as labeled slots.
Key Takeaway: Use children for flexible composition when you have one content area; use explicit named props when you need multiple distinct slots or want clearer component APIs.