Props as Slots: The Basic Pattern
You'll learn how to pass JSX elements as regular props to create multiple named insertion points in a component.
The Core Idea
Instead of relying on children alone, you can accept JSX elements through any prop name you want. This lets you create multiple, clearly-named "slots" where the parent can inject content. Think of it like a template with labeled blanks: header={...}, footer={...}, sidebar={...} — each prop is a named slot that accepts JSX.
Why This Works
In React, JSX elements are just objects. You can pass them as props exactly like you'd pass a string or number:
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>
);
}
// Usage
<Card
header={<h2>Welcome</h2>}
body={<p>This is the content area.</p>}
footer={<button>Close</button>}
/>
The Pattern's Power
This pattern shines when you need multiple content areas that are clearly separated. Instead of one generic children blob, you get:
- Clear naming:
headerimmediately tells you where content goes - Flexible ordering: The component controls layout, not insertion order
- Easy optionality: Missing props naturally render nothing
Key Takeaway: Props-as-slots treats JSX elements like any other data — pass them through named props to create multiple, clearly-labeled insertion points in your component's layout.