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

Passing Server Components as Children

A client component can accept server components via children prop, enabling composition without serialization issues.

Passing Server Components as Children

What You'll Learn

You'll discover how to compose Server and Client Components seamlessly using the children prop, bypassing serialization limitations.

The Core Problem and Solution

Remember that props crossing the Server-Client boundary must be serializable? This creates a dilemma: Client Components need interactivity (event handlers, state), but Server Components need backend access. How do you compose them together?

The answer: Pass Server Components as the children prop to Client Components.

When you do this, React doesn't serialize the Server Component itself. Instead, it renders the Server Component on the server first, then passes its already-rendered output to the Client Component. The Client Component receives a special React element reference (the Symbol.for('react.element') you learned about), not the component code itself.

How It Works in Practice

// ClientWrapper.tsx
'use client'

export function ClientWrapper({ children }: { children: React.ReactNode }) {
  const [isOpen, setIsOpen] = useState(true)
  
  return (
    <div>
      <button onClick={() => setIsOpen(!isOpen)}>Toggle</button>
      {isOpen && children}
    </div>
  )
}

// page.tsx (Server Component)
async function ServerData() {
  const data = await db.query('SELECT * FROM posts')
  return <ul>{data.map(...)}</ul>
}

export default function Page() {
  return (
    <ClientWrapper>
      <ServerData /> {/* Server Component as children! */}
    </ClientWrapper>
  )
}

The ClientWrapper gets interactivity, while ServerData keeps its direct database access—no serialization conflict.

The Mental Model

Think of it like a picture frame: the frame (Client Component) provides the interactive border and controls, but the picture inside (Server Component) was painted elsewhere and simply placed inside.

Key Takeaway: Passing Server Components as children to Client Components enables powerful composition patterns without serialization issues—the Server Component renders first, then its output slots into the Client Component's tree.