children · slot props
Composition with children and slots
A component that accepts children, or named slot props such as header and actions, lets its caller decide what goes inside without the component knowing about it. Composition is React’s default answer to reuse, and the first fix for prop drilling: the parent that has the data renders the child itself and passes it down as an element.
Use it for
- Layout components — cards, dialogs, page shells — that frame content they do not own.
- Avoiding prop drilling through components that only forward data they never read.
Watch for
- A component that grows a prop for every variation (showIcon, iconPosition, iconSize…) is asking to take a slot instead.
- Slots trade control for flexibility: the component can no longer guarantee what is inside it. Use them where callers genuinely differ.
function Card({ header, actions, children }: {
header: ReactNode
actions?: ReactNode
children: ReactNode
}) {
return (
<section className="card">
<header>
{header}
{actions}
</header>
{children}
</section>
)
}
<Card header={<h2>Invoices</h2>} actions={<ExportButton />}>
<InvoiceTable rows={rows} />
</Card>