What are Render Props?
Render props is a pattern where a component receives a function (often as children or a render prop) and calls it with its internal state or behavior to decide what to render: <MouseTracker>{({ x, y }) => <Cursor x={x} y={y} />}</MouseTracker>. It shares logic while leaving rendering to the caller. Custom hooks have replaced most uses, but the pattern is still useful for headless components and libraries.
How it works
- The component owns the logic (state, subscriptions, measurements).
- It calls
props.children(value)orprops.render(value)inside its JSX. - The caller returns whatever UI it wants for that value.
Where it still shines
- Headless UI and libraries: virtualizers (
react-windowrow renderers), Formik's<Formik>{(formik) => ...}</Formik>,react-error-boundary'sfallbackRender, Downshift. - Scoped access to context-like data at a specific place in the tree.
- Inversion of control: a list component lets callers render each item (
renderItem).
Drawbacks
- Nesting several render-prop components creates "callback pyramids".
- Inline functions are new each render, which can defeat memoization (less of an issue with the React Compiler).
- Less obvious to read than a hook call.
Hook equivalent
const { x, y } = useMousePosition() gives the same values with flat code. Use render props when the component also needs to control where or when something renders, or when the API is shared with class components.
Code example
1import { useEffect, useState } from "react";23type Size = { width: number; height: number };45// Render-prop component: owns the logic, caller decides the UI6export function WindowSize({ children }: { children: (size: Size) => React.ReactNode }) {7 const [size, setSize] = useState<Size>({ width: window.innerWidth, height: window.innerHeight });8 useEffect(() => {9 const onResize = () => setSize({ width: window.innerWidth, height: window.innerHeight });10 window.addEventListener("resize", onResize);11 return () => window.removeEventListener("resize", onResize);12 }, []);13 return <>{children(size)}</>;14}1516// A list that lets callers render each item (inversion of control)17export function List<T>({ items, renderItem }: { items: T[]; renderItem: (item: T, index: number) => React.ReactNode }) {18 return <ul>{items.map((item, i) => <li key={i}>{renderItem(item, i)}</li>)}</ul>;19}2021export function Demo() {22 return (23 <>24 <WindowSize>{({ width }) => <p>{width < 640 ? "Mobile" : "Desktop"} ({width}px)</p>}</WindowSize>25 <List items={["Ada", "Grace"]} renderItem={(name, i) => <strong>{i + 1}. {name}</strong>} />26 </>27 );28}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.