Course contentsShow
← React
Lesson 1591 of 2,39639. Server Components in PracticeFree lesson

The Slot Pattern with Server Components

Pass multiple server components to a client component via named props (header, sidebar) for flexible layouts.

The Slot Pattern with Server Components

What you'll learn: How to pass multiple server components to a client component through named props, creating flexible, composable layouts without crossing the serialization boundary.

The Core Idea

You've learned that server components can be passed as children to client components. But what if you need multiple insertion points? The slot pattern extends this idea by passing server components through named props like header, sidebar, or footer.

Think of it like a newspaper layout: the page frame (client component) defines where things go, but the actual articles (server components) are plugged into named slots. Each slot can be a different server component with its own data fetching, and none of their code ships to the browser.

Why Named Props?

// ❌ Limited: only one insertion point
<ClientLayout>
  <ServerContent />
</ClientLayout>

// ✅ Flexible: multiple named slots
<ClientLayout
  header={<ServerHeader />}
  sidebar={<ServerSidebar />}
  footer={<ServerFooter />}
>
  <ServerContent />
</ClientLayout>

Your client component receives these as props:

'use client'

export function ClientLayout({ header, sidebar, footer, children }) {
  const [collapsed, setCollapsed] = useState(false)
  
  return (
    <div>
      <nav>{header}</nav>
      <aside className={collapsed ? 'hidden' : ''}>
        {sidebar}
      </aside>
      <main>{children}</main>
      <footer>{footer}</footer>
    </div>
  )
}

The client component controls layout and interactivity (like the collapsible sidebar), while server components handle data and rendering for each slot.

Key Takeaway: The slot pattern lets you compose complex layouts by passing multiple server components as named props to client components, keeping your JavaScript bundle small while maintaining full layout control and interactivity.