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

Render Props for Conditional Logic Exposure

When a library needs to expose multiple states and let consumers decide how to render each, render props excel.

Render Props for Conditional Logic Exposure

What you'll learn: How render props let libraries expose multiple internal states while giving you full control over what to render in each case.

The Core Problem

Imagine you're building a reusable component that fetches data. Internally, it knows if it's loading, if an error occurred, or if the data succeeded. But how should it decide what UI to show for each state? Should it render a spinner? An error message? What color? What text?

This is where render props shine: instead of the library making UI decisions, it exposes conditional states and lets you decide what to render.

How It Works

The library component tracks all its internal states, then calls your render prop function with an object describing the current situation:

<DataFetcher url="/api/users">
  {({ loading, error, data }) => {
    if (loading) return <Spinner />;
    if (error) return <ErrorPanel message={error.message} />;
    return <UserList users={data} />;
  }}
</DataFetcher>

The DataFetcher component doesn't know or care what a Spinner looks like—it just tells you "hey, I'm loading" and you handle the rest.

Why This Pattern Excels

This pattern gives maximum flexibility without configuration bloat. Instead of the library accepting props like loadingComponent, errorComponent, spinnerColor, etc., it simply exposes the raw conditional states and trusts you to compose your UI.

It's like a weather API that gives you "temperature, humidity, wind speed" instead of trying to decide whether you need an umbrella.

Key Takeaway: When a library needs to expose complex conditional states (loading, error, success, empty, etc.), render props provide a clean way to give consumers complete rendering control without drowning in configuration props.