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

Combining Function Children with Other Props

Mixing function children with regular props to create flexible APIs that support multiple usage patterns.

Combining Function Children with Other Props

What you'll learn: How to use function children alongside regular props to create flexible component APIs that support multiple usage patterns.

The Core Idea

You don't have to choose between function children or regular props—you can use both together! A component can accept a function as its children prop while also receiving other props that customize behavior or provide additional data. This creates a flexible API where the function children handle rendering logic, while other props control behavior, configuration, or provide fallback content.

Why Combine Them?

Think of it like a customizable restaurant menu: the function children let you decide how each dish is presented, while other props let you filter by dietary preferences, set portion sizes, or provide a default message when nothing matches.

Here's a practical example:

function DataFetcher({ url, loading, error, children }) {
  const [data, setData] = useState(null);
  const [isLoading, setIsLoading] = useState(true);
  const [fetchError, setFetchError] = useState(null);

  // ... fetch logic ...

  if (isLoading) return loading || <div>Loading...</div>;
  if (fetchError) return error || <div>Error: {fetchError.message}</div>;
  
  return children(data);
}

// Usage with custom loading state
<DataFetcher 
  url="/api/users" 
  loading={<Spinner />}
>
  {users => <UserList users={users} />}
</DataFetcher>

The function child (children) handles rendering the successful data state, while the loading and error props provide customizable fallbacks. Users can override defaults selectively—just the loader, just the error, or neither.

This pattern creates a progressive enhancement API: basic usage works out of the box, but you can layer in customization wherever needed.

Key Takeaway: Combining function children with regular props creates powerful, flexible APIs where function children handle core rendering logic while other props provide configuration, fallbacks, and behavior customization.