React Render Props Pattern
We’ve all been there: you need to fetch data from an API, so you write your standard block of three useState hooks (for data, loading, and errors) paired with a useEffect hook.
Then, you need to fetch data from a different endpoint on the same page. What do you do? Usually, you copy, paste, and frustratingly rename all those states. This is a classic “code smell.”
Get Mr Prime’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s look at how the Render Props Pattern cleans up this mess by separating your network logic from your UI.
The Messy Way: State Explosion
Without a design pattern, fetching a User Profile and a Todo List in the same dashboard forces you to duplicate your lifecycle logic. Notice how bloated this component gets just handling two API calls:
import { useState, useEffect } from "react";
export default function Dashboard() {
// ❌ Duplicate State Set 1 (Profile)
const [profileData, setProfileData] = useState(null);
const [isProfileLoading, setIsProfileLoading] = useState(true);
const [profileError, setProfileError] = useState(null);
useEffect(() => {
fetch("https://api.github.com/users/octocat")
.then(res => res.json()).then(data => { setProfileData(data); setIsProfileLoading(false); })
.catch(err => { setProfileError(err.message); setIsProfileLoading(false); });
}, []);
// ❌ Duplicate State Set 2 (Todos) - Forced to rename variables to avoid conflicts!
const [todoData, setTodoData] = useState(null);
const [isTodoLoading, setIsTodoLoading] = useState(true);
const [todoError, setTodoError] = useState(null);
useEffect(() => {
fetch("https://jsonplaceholder.typicode.com/todos?_limit=3")
.then(res => res.json()).then(data => { setTodoData(data); setIsTodoLoading(false); })
.catch(err => { setTodoError(err.message); setIsTodoLoading(false); });
}, []);
return (
<div>
<h2>Dashboard (The Messy Way)</h2>
{/* Profile UI */}
<div>
{isProfileLoading && <p>Loading...</p>}
{profileData && <p>Bio: {profileData.bio}</p>}
</div>
{/* Todo UI */}
<div>
{isTodoLoading && <p>Loading...</p>}
{todoData && <ul>{todoData.map(t => <li key={t.id}>{t.title}</li>)}</ul>}
</div>
</div>
);
}The Better Way: The Render Props Pattern
Instead of duplicating code, we can abstract the data-fetching lifecycle into a single, reusable component. This component runs the logic and then executes a render function prop, handing the data right back to the UI layout.
1. The Reusable Wrapper (FetchData.jsx)
import { useState, useEffect } from "react";
export function FetchData({ url, render }) {
const [data, setData] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
setIsLoading(true);
fetch(url)
.then(res => { if (!res.ok) throw new Error("Error fetching data"); return res.json(); })
.then(data => { setData(data); setIsLoading(false); })
.catch(err => { setError(err.message); setIsLoading(false); });
}, [url]);
// Execute the render function passing the state parameters downstream
return render({ data, isLoading, error });
}2. The Clean Dashboard (Dashboard.jsx)
Now your dashboard component doesn’t need a single hook. It simply drops in the <FetchData/> wrapper and declares exactly what the UI should look like on the fly:
import { FetchData } from "./FetchData";
export default function Dashboard() {
return (
<div style={{ padding: "20px" }}>
<h2>Dashboard (Render Props Way)</h2>
{/* Profile Container */}
<FetchData
url="https://api.github.com/users/octocat"
render={({ data, isLoading, error }) => {
if (isLoading) return <p>Loading profile...</p>;
if (error) return <p>Error: {error}</p>;
return <div className="card"><h4>{data.name}</h4><p>{data.bio}</p></div>;
}}
/>
<hr />
{/* Tasks Container */}
<FetchData
url="https://jsonplaceholder.typicode.com/todos?_limit=3"
render={({ data, isLoading, error }) => {
if (isLoading) return <p>Loading tasks...</p>;
if (error) return <p>Error: {error}</p>;
return <ul>{data.map(todo => <li key={todo.id}>{todo.title}</li>)}</ul>;
}}
/>
</div>
);
}Why This Matters
By switching to the Render Props pattern:
- You eliminate naming gymnastics: No more creating prefix variations like
profileDataandtodoData. - Infinite Reusability: If you need to add a third section for “Weather Updates,” you simply drop in a new
<FetchData/>component and pass it a custom layout function. - Centralized Maintenance: If you want to swap out the native browser
fetchfor a library likeaxios, you only have to modify it in one file (FetchData.jsx), rather than altering every screen across your application tree.




