Render Props is a pattern where a component receives a function through a prop and calls it for rendering. Appeared before hooks, now almost not used.
Simple analogy: Imagine you are ordering pizza with different fillings.
What it looks like:
1// Component that tracks mouse position2class MouseTracker extends React.Component {3 state = { x: 0, y: 0 };45 handleMouseMove = (e) => {6 this.setState({ x: e.clientX, y: e.clientY });7 };89 render() {10 return (11 <div onMouseMove={this.handleMouseMove}>12 {this.props.render(this.state)} {/* Calling render prop! */}13 </div>14 );15 }16}1718// Usage19<MouseTracker render={({ x, y }) => (20 <p>Position: {x}, {y}</p>21)} />
Why this is outdated: Hooks do the same thing simpler:
1function useMousePosition() {2 const [pos, setPos] = useState({ x: 0, y: 0 });3 useEffect(() => {4 const handler = (e) => setPos({ x: e.clientX, y: e.clientY });5 window.addEventListener('mousemove', handler);6 return () => window.removeEventListener('mousemove', handler);7 }, []);8 return pos;9}1011function Component() {12 const { x, y } = useMousePosition();13 return <p>Position: {x}, {y}</p>;14}
When it might be useful: If you are maintaining old class component code. For new projects use custom hooks.