Course contentsShow
← React
Lesson 1160 of 2,39630. Composition: Inversion of ControlFree lesson

Render Customization: Beyond Children

Using render props for specific sub-sections to let consumers control parts of the UI independently.

Render Customization: Beyond Children

You'll learn how to give consumers fine-grained control over specific UI sections using targeted render props instead of a single children slot.

The Core Idea

Sometimes children isn't enough. You might need to let consumers customize multiple specific parts of your component independently—like a modal's header, footer, and body. Rather than forcing everything through one children prop or creating dozens of separate slot props, render props let you expose named render functions that consumers can implement for precise control.

Think of it like a restaurant kitchen with multiple stations. Instead of one "food" prop that handles everything, you have separate functions for appetizers, mains, and desserts—each station can be customized independently with full access to the ingredients (state/data) they need.

The Pattern

function DataTable({ data, renderHeader, renderRow, renderEmpty }) {
  if (data.length === 0) {
    return renderEmpty ? renderEmpty() : <p>No data</p>;
  }

  return (
    <table>
      {renderHeader && <thead>{renderHeader()}</thead>}
      <tbody>
        {data.map((item) => (
          <tr key={item.id}>{renderRow(item)}</tr>
        ))}
      </tbody>
    </table>
  );
}

Now consumers control each section separately:

<DataTable
  data={users}
  renderHeader={() => (
    <tr>
      <th>Name</th>
      <th>Email</th>
    </tr>
  )}
  renderRow={(user) => (
    <>
      <td>{user.name}</td>
      <td>{user.email}</td>
    </>
  )}
  renderEmpty={() => <p>No users found. Try adjusting filters.</p>}
/>

Each render prop receives the relevant data it needs (like item in renderRow) and returns JSX for that specific section.

Key Takeaway: Multiple render props let you expose granular customization points where consumers need control over specific UI sections with access to relevant data, going beyond the single-slot limitation of children.