Lesson 24 of 80
Sharing logic by passing a function as a prop that returns JSX — the pre-hooks answer to reusable stateful logic, still worth recognizing even though custom hooks replaced most of its use cases.
11 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
In `<MouseTracker render={(pos) => <p>{pos.x}</p>} />`, which component owns the mouse-position state?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
A render prop is a prop whose value is a function that returns JSX — the component calls that function during its own render, handing it whatever data/state it manages internally:
function MouseTracker({ render }) {
const [pos, setPos] = useState({ x: 0, y: 0 });
return (
<div onMouseMove={(e) => setPos({ x: e.clientX, y: e.clientY })}>
{render(pos)} {/* MouseTracker owns the state, caller decides what to render with it */}
</div>
);
}
<MouseTracker render={(pos) => <p>{pos.x}, {pos.y}</p>} />
MouseTracker owns the mouse-tracking logic (the state, the event listener); the caller supplies how to display it. This is exactly the same goal as a custom hook — reusable stateful logic, decoupled from any specific UI — solved with a different mechanism, before hooks existed.
<DataFetcher render={(data) => <List items={data} />} /> — a prop whose value is a function.
"How did people share stateful logic between components before hooks existed?" checks whether your React knowledge goes back far enough to recognize render props and HOCs as real, still-encountered patterns — useful for reading legacy code even in an all-hooks codebase.