Course contentsShow
← React
Lesson 276 of 2,3968. Props PatternsFree lesson

Basic Render Prop Implementation

Creating a component that accepts a render function prop and calls it with data to produce UI.

Basic Render Prop Implementation

What you'll learn: How to create a component that accepts a function as a prop and calls it to render UI with data.

The Core Idea

A render prop is simply a prop whose value is a function. Your component calls this function and uses whatever JSX it returns. This lets the parent decide what to render while your component provides the data to render with.

Think of it like a restaurant kitchen: your component (the kitchen) prepares the ingredients (data), but the render prop function (the chef's plating instructions) decides how to arrange them on the plate.

Basic Implementation Pattern

function DataProvider({ render }) {
  const data = "Hello from DataProvider";
  
  return render(data);
}

// Usage:
function App() {
  return (
    <DataProvider 
      render={(data) => <h1>{data}</h1>}
    />
  );
}

Here's what happens:

  1. DataProvider receives a render prop (a function)
  2. It prepares some data ("Hello from DataProvider")
  3. It calls the render function, passing the data as an argument
  4. It returns whatever JSX the render function produces

Why This Pattern Matters

The parent component gets complete control over presentation while the DataProvider handles the logic and data. You can reuse DataProvider with totally different UIs:

<DataProvider render={(data) => <p className="text-small">{data}</p>} />
<DataProvider render={(data) => <div><strong>{data}</strong></div>} />

Same component, different renders!

Key Takeaway: A render prop component accepts a function prop, calls it with data, and returns the result—separating data logic from presentation decisions.