Sub topic 12
Composition: slot props and render props
This chapter assumes you know how children, callback props, and the difference between props and state work. You should be comfortable with JSX and with passing elements and functions as prop values.
Composition over configuration
A component with many options (showHeader, headerText, headerIcon, and so on) gets hard to use. React's alternative is composition. The parent passes in pieces of UI, and the child decides where to place them. Because props can hold any value, including elements and functions, two patterns cover most cases: slot props and render props.
Slot props
The children prop gives a component one slot. When a layout needs several areas, give each area its own named prop and pass an element as its value.
function Layout({ header, sidebar, children }) {
return (
<div>
<header>{header}</header>
<aside>{sidebar}</aside>
<main>{children}</main>
</div>
);
}
export default function App() {
return (
<Layout header={<h1>Dashboard</h1>} sidebar={<nav>Menu</nav>}>
<p>Main content</p>
</Layout>
);
}Layout knows where each area goes. It does not know what fills it. The parent can swap <nav>Menu</nav> for any other element without changing Layout.
Slot props are optional like any other prop. If the parent leaves out sidebar, its value is undefined, and React renders nothing for it. The empty <aside> stays in the output, though. Guard it when that matters:
{sidebar && <aside>{sidebar}</aside>}Render props
A slot holds finished UI. Sometimes the child has data the parent needs in order to build that UI. In that case the parent passes a function, and the child calls it with the data. This is a render prop.
import { useState } from 'react';
function Toggle({ render }) {
const [on, setOn] = useState(false);
return render(on, () => setOn(!on));
}
export default function App() {
return (
<Toggle
render={(on, toggle) => (
<button onClick={toggle}>{on ? 'On' : 'Off'}</button>
)}
/>
);
}Toggle owns the state and the logic. The parent owns the markup. A second parent could render the same Toggle as a checkbox or a switch.
The prop does not have to be called render. A function placed between the tags is also common and is received as children, so children is then a function: {children(on, toggle)}.
Choosing between them
| Need | Pattern |
|---|---|
| One block of nested content | `children` |
| Several fixed areas | Slot props |
| The child must hand data or state to the parent's markup | Render prop |
Use the simplest pattern that works. Slots are plain elements and easy to read. Render props add a function call, which is only worth it when the child has data to share.
Common errors
- Passing a component instead of an element.
header={Header}passes a function, and React cannot render it as a child. Writeheader={<Header />}. - Defining the render function inline and expecting stable identity. An inline arrow is a new function on every render. That matters if the child is wrapped in
memo, because the function prop will always differ. - Calling the render prop when it is missing. If
renderis optional, check it first or provide a default. - Using a render prop where a hook would do. If a component only shares logic and has no markup of its own, a custom hook is often simpler and avoids the extra nesting.
Exercise
A Panel component needs a title area and a footer area, and the parent also wants to put arbitrary content in the middle. Which props would you give Panel, and what would the parent write to fill the title with an <h2>?
Show answer
Give Panel the props title, footer, and children. The parent writes <Panel title={<h2>Settings</h2>} footer={<button>Save</button>}><p>Body</p></Panel>. Panel places {title}, {children}, and {footer} where it wants them. No render prop is needed because Panel has no data to share with the parent.
Exercise
In the Toggle example, what must the parent do to display the text "Enabled" or "Disabled" instead of a button?
Show answer
Pass a different function to render, for example render={(on) => <p>{on ? 'Enabled' : 'Disabled'}</p>}. Toggle does not change, because it only calls render(on, toggle) and returns the result.