Course contentsShow
← React
Lesson 1126 of 2,39629. Composition: SlotsFree lesson

Slot Props Pattern: Layout Components

Build layout components that accept header, main, and sidebar slots for flexible page structures.

Slot Props Pattern: Layout Components

You'll learn how to create reusable layout components that accept separate content slots for different page regions like headers, sidebars, and main content.

The Layout Problem

When building pages, you often need the same structural wrapper—a header at top, sidebar on left, main content in center. Without slots, you'd either duplicate layout code everywhere or cram everything into a single children prop, losing control over where content appears.

Building with Named Slots

Layout components use multiple props as "slots" to receive different pieces of content:

function PageLayout({ header, sidebar, main }) {
  return (
    <div className="page-layout">
      <header className="page-header">
        {header}
      </header>
      
      <div className="page-body">
        <aside className="sidebar">
          {sidebar}
        </aside>
        
        <main className="main-content">
          {main}
        </main>
      </div>
    </div>
  );
}

// Usage
function ProfilePage() {
  return (
    <PageLayout
      header={<h1>User Profile</h1>}
      sidebar={<nav>Navigation items...</nav>}
      main={<div>Profile content here</div>}
    />
  );
}

The Flexibility Advantage

Think of it like a building blueprint with labeled rooms. Each slot (header, sidebar, main) defines where content goes, while consumers control what fills each space. You can reuse the same layout structure across dozens of pages with completely different content.

Optional slots work naturally too—if sidebar isn't provided, you might render a full-width main area instead.

Key Takeaway: Layout components with named slot props separate page structure from page content, letting you build consistent layouts once and populate them differently everywhere, with each slot's placement handled by the component itself.