Sub topic 3
Render props
This chapter assumes you know function components, props, JSX, useState, and how to pass functions as props. It covers the render props pattern: what it is, how to use it, and where it goes wrong.
The idea
A render prop is a prop whose value is a function that returns React elements. The component that receives it calls the function and renders the result. The component owns the logic and state. The caller owns the output.
The most common form uses children as the function. This is sometimes called "function as a child".
import { useState } from 'react';
function Toggle({ children }) {
const [on, setOn] = useState(false);
return children({ on, toggle: () => setOn((prev) => !prev) });
}
export function App() {
return (
<Toggle>
{({ on, toggle }) => (
<button onClick={toggle}>{on ? 'On' : 'Off'}</button>
)}
</Toggle>
);
}Toggle keeps the on state and builds a toggle function. It never decides what appears on screen. App receives both values as an argument and returns a button. A different caller could return a checkbox, an icon, or a whole panel from the same Toggle.
How the data flows
The function runs on every render of Toggle. When state changes, Toggle re-renders and calls your function again with the new values.
Using a named prop
children is not required. A named prop works the same way and lets you pass more than one render function.
function List({ items, renderItem, renderEmpty }) {
if (items.length === 0) return renderEmpty();
return <ul>{items.map((item) => renderItem(item))}</ul>;
}Here the caller supplies renderItem and renderEmpty. Remember that elements returned in a list need a key, so the caller's renderItem should return an element with one, for example <li key={item.id}>.
Common errors
- Passing something that is not a function. If a caller writes
<Toggle><button /></Toggle>, thenchildren(...)throws "children is not a function". Validate the prop or document the expected shape. - Creating deep nesting. Combining several render prop components produces nested callbacks that are hard to read.
- Breaking memoization. An inline arrow function is a new object on every render. If the receiving component is wrapped in
React.memo, the new function prop makes the memo check fail each time. - Forgetting to return. Using braces in the arrow function without
returnproducesundefined, and the component renders nothing.
When to use it
Use a render prop when a component must control when and how often something renders, and the caller must control what it looks like. If you only need to share stateful logic with no rendering on the provider's side, prefer a custom hook, which the next chapters cover.
Exercise
Why does <Toggle><button>Click</button></Toggle> throw an error when Toggle is written as in this chapter, and how would you make Toggle fail with a clearer message?
Show answer
Toggle calls children({ ... }), but here children is a React element object, not a function, so JavaScript throws "children is not a function". To give a clearer message, check typeof children !== 'function' at the top of Toggle and throw an Error such as "Toggle expects a function as its child".
Exercise
A render prop component is wrapped in React.memo, yet it re-renders every time its parent renders. The parent passes renderItem={(item) => <li key={item.id}>{item.name}</li>}. What causes this, and what is one fix?
Show answer
The inline arrow function is created anew on each parent render, so the prop changes identity and React.memo sees a different value. Fix it by defining the function outside the component, or by wrapping it in useCallback so its identity stays the same between renders.