React › Render Props Pattern
What is a common naming alternative to render props, using children as a function instead?
IntermediateAnswer
Instead of a named prop called render, some components accept children as a function directly, which reads a bit more naturally in JSX since it looks like normal nested content, even though it is technically still a function being called by the parent component.
function DataProvider({ children }) {
const [data, setData] = useState(null);
useEffect(() => { fetchData().then(setData); }, []);
return children(data);
}
<DataProvider>
{(data) => (data ? <p>{data.name}</p> : <p>Loading</p>)}
</DataProvider>Real-world example A shared DataProvider component uses children as a function so that using it in JSX still looks like normal nested content, even though it is actually calling a function to decide exactly what gets displayed.