A render prop is a prop whose value is a function that returns React elements. The component calls it to delegate part of its rendering to the caller, while it supplies the data or behaviour. children as a function is the most common form: <Mouse>{pos => <Cursor {...pos} />}</Mouse>. It was a key pattern for sharing stateful logic with caller-controlled UI before hooks. Today most logic sharing uses custom hooks, but render props are still the right tool when a component must control where or when caller UI renders: list item renderers, virtualized rows, headless components, and slots that need internal state.
function DataList({ items, renderItem, renderEmpty = () => <p>No results</p> }) {
if (items.length === 0) return renderEmpty();
return (
<ul>
{items.map((item, index) => (
<li key={item.id}>{renderItem(item, { index, isLast: index === items.length - 1 })}</li>
))}
</ul>
);
}
<DataList
items={users}
renderItem={(user, { isLast }) => <UserRow user={user} divider={!isLast} />}
/>
DataList owns the structure (list semantics, keys, the empty state) and the caller owns each item's look.
function Toggle({ children }) {
const [on, setOn] = useState(false);
return children({ on, toggle: () => setOn(o => !o) });
}
<Toggle>
{({ on, toggle }) => (
<button aria-pressed={on} onClick={toggle}>{on ? 'On' : 'Off'}</button>
)}
</Toggle>
The same Toggle as a hook:
function useToggle(initial = false) {
const [on, setOn] = useState(initial);
return { on, toggle: useCallback(() => setOn(o => !o), []) };
}
Hooks avoid nesting ("callback pyramids" of several render-prop components), don't add components to the tree, and type more easily. Prefer hooks when the caller just needs values.
Render props still win when the provider needs to render the result itself:
<List rowCount={10000}>{({ index, style }) => <Row style={style} … />}</List>. Only the library knows which rows to render, and where.<Field render>).<Formik> child function.children/slots) instead.memo on the receiving component (a new function each render). It's usually fine. Memoize, or rely on the React Compiler, if it matters.<renderItem />) is wrong. It's a function, so call it.Spotted a mistake or a rights concern? Report an issue