Course contentsShow
← React
Lesson 1045 of 2,39627. Composition: Render PropsFree lesson

Passing Data Through the Render Function

The component calls your render function with arguments, exposing its internal state or computed values to the consumer.

Passing Data Through the Render Function

What you'll learn: How a component exposes its internal state or computed values to consumers by passing arguments to the render function.


The Core Idea

You've seen that a render prop is a function passed as a prop. Now here's the powerful part: the component doesn't just call your function—it calls it with arguments. This is how the component shares its internal state, methods, or computed values with you.

Think of it like a vending machine that doesn't just dispense a product—it hands you the product along with the receipt, nutritional info, and a coupon. The component is saying: "Here's everything I know that you might need."

How It Works

When the component calls your render function, it passes data as arguments:

function MouseTracker({ render }) {
  const [position, setPosition] = useState({ x: 0, y: 0 });

  // Component passes its state to YOUR function
  return render(position);
}

// Usage: You receive the data
<MouseTracker 
  render={(mousePosition) => (
    <p>Mouse is at {mousePosition.x}, {mousePosition.y}</p>
  )}
/>

The MouseTracker component exposes position by passing it as an argument. You receive mousePosition in your render function and can use it however you need.

You can pass multiple values:

return render(position, setPosition, isMoving);

Or bundle them in an object:

return render({ position, setPosition, isMoving });

Why This Matters

This pattern lets the component own and manage the logic while letting you decide how to present it. The component handles the "what" (tracking mouse position), you handle the "how" (displaying it).

Key Takeaway: Render props become powerful when the component passes arguments to your function, exposing its internal state and letting you use that data however you need.