Sub topic 10
Props as Slots
This chapter assumes you know function components, props, JSX, and how children works. It covers passing elements through named props so a parent can place content in specific regions of a component.
The idea
The children prop gives a component one open area. Many components need several: a card with a header, a body, and a footer. A slot is a prop whose value is JSX. The component decides where the slot renders, and the caller decides what goes in it.
function Card({ header, footer, children }) {
return (
<section>
{header && <header>{header}</header>}
<div>{children}</div>
{footer && <footer>{footer}</footer>}
</section>
);
}
export function Example() {
return (
<Card
header={<h2>Invoice</h2>}
footer={<button>Pay now</button>}
>
<p>Total due: $42</p>
</Card>
);
}Card owns the layout and the wrapper elements. The caller owns the content. Card never needs to know that the header is an h2 or that the footer holds a button.
Slots versus compound components
Both patterns let the caller supply pieces of the UI. They differ in how the pieces are placed.
| Aspect | Props as slots | Compound components |
|---|---|---|
| Placement | Fixed by the parent | Chosen by the consumer |
| Shared state | None, or passed manually | Shared through context |
| Call site | One element with named props | Several nested elements |
| Best for | Fixed layout regions | Coordinated, reorderable parts |
With Tabs, the consumer can put the tab list above or below the panels. With Card, the header always comes first. If the layout should stay fixed, slots are simpler and need no context.
Slots that need data
A slot can also be a function. This is the render prop pattern applied to one region. Use it when the slot content depends on state the parent owns.
import { useState } from 'react';
function Panel({ title, actions }) {
const [open, setOpen] = useState(true);
return (
<section>
<h2>{title}</h2>
{actions({ open, toggle: () => setOpen((prev) => !prev) })}
{open && <p>Panel content</p>}
</section>
);
}
export function App() {
return (
<Panel
title="Details"
actions={({ open, toggle }) => (
<button onClick={toggle}>{open ? 'Hide' : 'Show'}</button>
)}
/>
);
}Only use a function slot when the slot needs data. A plain element is easier to read.
Common errors
- Empty wrappers. Rendering
<header>{header}</header>without checkingheaderleaves an empty element in the DOM. Guard it as shown inCard. - Falsy values.
header={0}fails aheader &&check and renders0. Useheader != nullwhen zero is a valid value. - Too many slots. If a component has six or seven slot props, the call site becomes hard to scan. Consider compound components instead.
- Unstable function slots. An inline function slot is recreated on every render. This is fine in most cases. It matters only if the child is memoized.
Summary
Slots give you named regions with a fixed layout and no context. They sit between a single children prop and full compound components. Pick them when the structure is stable.
Exercise
A Modal always shows a title area, then body content, then a button row. Callers only change the content of each area. Would you use slots or compound components, and why?
Show answer
Slots. The layout is fixed and no state needs to be shared between the parts, so named props such as title and actions plus children for the body are enough. Compound components with context would add complexity without giving callers anything they need.