The Render Props Pattern in JavaScript Explained
Render props let a React component share logic by calling a function prop that returns UI. See React 19 examples, children functions and the hooks alternative.

A render prop is a prop whose value is a function that a component calls to decide what to render. The component owns some logic, such as tracking the mouse or loading data, and hands the result to your function, which returns the markup. It was the standard way to share stateful logic between React components before hooks, and it still has uses. This post shows the pattern with React 19, using renderToStaticMarkup so every output can be read from the code, covers the children as a function variant, the pitfalls, and where hooks now do the job better.
The problem: logic and markup are tangled
Say you need a toggle that tracks an open or closed state. Used in a dropdown, a modal and an accordion, the same logic with different markup keeps appearing. Copying the state code into each component duplicates it, and wrapping the markup inside the toggle component forces one look on everyone.
A render prop splits them: the toggle owns the state and gives the caller the current value and a way to change it, and the caller decides what to draw.
A first render prop
This component keeps an on flag and passes it, with a toggle function, to whatever function you supply.
import { useState } from "react";
export function Toggle({ initial = false, render }) {
const [on, setOn] = useState(initial);
return render({ on, toggle: () => setOn((value) => !value) });
}The caller supplies render, which receives an object and returns elements.
import { renderToStaticMarkup } from "react-dom/server";
import { Toggle } from "./Toggle.jsx";
const menu = (
<Toggle
initial={true}
render={({ on, toggle }) => (
<div>
<button onClick={toggle}>Menu</button>
{on && <ul><li>Profile</li><li>Sign out</li></ul>}
</div>
)}
/>
);
console.log(renderToStaticMarkup(menu));<div><button>Menu</button><ul><li>Profile</li><li>Sign out</li></ul></div>The server render shows the open state, because initial is true. Event handlers such as onClick do not appear in static markup, and in a browser clicking the button would call toggle and re-render. The same Toggle can drive a modal by passing a different render, and the state logic is written once.
Children as a function
The prop does not have to be named render. A common variant uses children as the function, which reads naturally in JSX.
import { useState } from "react";
export function Counter({ children }) {
const [count, setCount] = useState(0);
return children({ count, increment: () => setCount((c) => c + 1) });
}
export const view = (
<Counter>
{({ count }) => <p>Clicked {count} times</p>}
</Counter>
);import { renderToStaticMarkup } from "react-dom/server";
import { view } from "./Counter.jsx";
console.log(renderToStaticMarkup(view));<p>Clicked <!-- -->0<!-- --> times</p>The comment nodes appear because React separates adjacent text and expression values in server output, so the browser can hydrate them as distinct text nodes. They are harmless. Whether you name the prop render or use children, the mechanism is the same: the component calls your function with its internal state.
A realistic case: loading data
A data-loading component is the classic example. It owns the loading and error states, and the caller owns what each looks like.
import { useEffect, useState } from "react";
export function Fetcher({ load, children }) {
const [state, setState] = useState({ status: "loading", data: null, error: null });
useEffect(() => {
let cancelled = false;
load()
.then((data) => !cancelled && setState({ status: "done", data, error: null }))
.catch((error) => !cancelled && setState({ status: "error", data: null, error }));
return () => {
cancelled = true;
};
}, [load]);
return children(state);
}import { renderToStaticMarkup } from "react-dom/server";
import { Fetcher } from "./Fetcher.jsx";
const loadUsers = () => Promise.resolve(["Ada", "Lin"]);
const page = (
<Fetcher load={loadUsers}>
{({ status, data }) =>
status === "loading" ? <p>Loading</p> : <ul>{data.map((n) => <li key={n}>{n}</li>)}</ul>
}
</Fetcher>
);
console.log(renderToStaticMarkup(page));<p>Loading</p>Server rendering does not run effects, so the output is the initial loading state. In a browser the effect runs, loadUsers resolves, and the component re-renders with the list. Notice that load is a stable function defined outside the component. Defining it inline would give the effect a new dependency on every render and cause an endless fetch loop, one of the main traps of this pattern.
Pitfall 1: a new function on every render
A render prop is usually an inline arrow function, so it is a new function each time the parent renders. That is harmless for a plain component. It breaks React.memo and PureComponent optimizations, because the prop always looks changed, and it matters when the receiving component uses the function in an effect dependency list.
import { memo } from "react";
const Expensive = memo(function Expensive({ render }) {
return render("data");
});
// every parent render creates a new function, so memo never skips
const bad = <Expensive render={(d) => <p>{d}</p>} />;If memoization matters, define the function once with useCallback, or move it outside the component when it needs no props. The better question is whether the cost is measurable; in most trees it is not.
Pitfall 2: nesting
Combining several render-prop components nests the JSX deeper with each one.
const screen = (
<Auth>
{(user) => (
<Theme>
{(theme) => (
<Locale>
{(locale) => <p>{user.name} {theme.mode} {locale.code}</p>}
</Locale>
)}
</Theme>
)}
</Auth>
);Each level indents the next, and values from outer functions are only visible inside the inner ones. This pyramid was the main complaint that led to hooks.
Hooks replaced most uses
A custom hook shares stateful logic without a wrapper component, and without nesting.
import { useState } from "react";
export function useToggle(initial = false) {
const [on, setOn] = useState(initial);
return [on, () => setOn((value) => !value)];
}
export function Menu() {
const [on, toggle] = useToggle(true);
return (
<div>
<button onClick={toggle}>Menu</button>
{on && <ul><li>Profile</li></ul>}
</div>
);
}import { renderToStaticMarkup } from "react-dom/server";
import { Menu } from "./useToggle.jsx";
console.log(renderToStaticMarkup(<Menu />));<div><button>Menu</button><ul><li>Profile</li></ul></div>The output matches the render-prop version, and the component reads top to bottom. For logic that does not involve rendering, a hook is simpler and composes without nesting.
Where render props are still the right tool
Hooks share logic, but they cannot decide what a child component's markup is, and they cannot wrap a region of the tree. Render props remain useful when the component must control where and how often the callback runs.
Lists with per-item rendering. A virtualized list calls
renderItem(item, index)only for visible rows, which is something only the list knows. Libraries such as React Native'sFlatListand many virtualization packages expose this prop.Components that own a layout, with slots you fill. A table component that calls
renderCellfor each column keeps layout control and lets you decide the content.Headless UI components. Libraries that provide behavior with no styling, such as a combobox, may pass state through a render prop or a function child, so you supply all the markup.
Libraries that must work with class components. A render prop works in any React version and without hooks.
function List({ items, renderItem }) {
return <ul>{items.map((item, i) => <li key={item.id}>{renderItem(item, i)}</li>)}</ul>;
}
const page = (
<List
items={[{ id: 1, name: "Ada" }, { id: 2, name: "Lin" }]}
renderItem={(item, i) => `${i + 1}. ${item.name}`}
/>
);import { renderToStaticMarkup } from "react-dom/server";
console.log(renderToStaticMarkup(page));<ul><li>1. Ada</li><li>2. Lin</li></ul>The list decides when to call renderItem and in what container, and the caller decides what each row contains. A hook cannot invert control this way.
Naming and typing
If you pass one function, render or children is clear. For several, name each for what it draws: renderHeader, renderEmpty. In TypeScript, type the function's argument so callers get checked access to the state: render: (state: { on: boolean; toggle: () => void }) => ReactNode.
Testing
Test the component with a render function that captures what it receives, and test the markup you pass separately.
import assert from "node:assert/strict";
import { test } from "node:test";
import { renderToStaticMarkup } from "react-dom/server";
import { Toggle } from "./Toggle.jsx";
test("Toggle passes its initial state to render", () => {
let received;
renderToStaticMarkup(
<Toggle initial={true} render={(state) => { received = state; return null; }} />,
);
assert.equal(received.on, true);
assert.equal(typeof received.toggle, "function");
});✔ Toggle passes its initial state to render (2.1ms)
ℹ tests 1
ℹ pass 1
ℹ fail 0Reading render props in existing code
You will meet render props in older codebases and in libraries that predate hooks, so it helps to read them quickly. Look for a component that takes a function as children or as a prop named render, renderItem, component or similar. That function is called by the component with a value, and what it returns is placed in the output. Trace what the component passes in, since that is the API: the argument object is the whole contract between the two sides.
When migrating such code to hooks, move the state logic into a custom hook with the same arguments, return the same values the render function received, and replace the wrapper with a call to the hook. The migration is mechanical, and the resulting component usually loses one or two levels of indentation. Keep the render-prop component as a thin adapter if other code still uses it, implemented by calling the hook and passing its result to the function, so both styles work during the transition.


