Nextjs Fundamentals · Server & Client Components

Interleaving server and client

composition via `children`

Interleaving server and client

You will often want a Client Component — say an interactive tabs or search widget — to render server-fetched content. But you cannot import a Server Component into a Client Component: the import drags the child across the boundary and it becomes a Client Component too.

The answer is composition. Pass the Server Component into the Client Component as a prop, most commonly children:

// app/dashboard/page.tsx — a Server Component
import { Tabs } from './tabs'; // a Client Component

export default async function Page() {
  const data = await getData();
  return (
    <Tabs>
      <ServerChart data={data} />
    </Tabs>
  );
}

Tabs is a Client Component that renders {children}. ServerChart is written in the parent — a Server Component — so it is rendered on the server and passed in as already-rendered content. Tabs wraps interactive chrome around it without ever owning the server code.

This is the general rule: Server Components can render Client Components, and a Server Component can pass another Server Component to a Client Component through props. The child keeps its server identity because the parent that created it is a Server Component. Props crossing the boundary must still be serializable, which is the next Lesson.

You are about to answer a short quiz on composition via children.

  1. You can import a Server Component directly into a Client Component.
  2. Pass Server Components into Client Components through props such as ___, so they stay Server Components.
  3. When a Server Component passes another Server Component as `children`, the child still renders on the server.
  4. Composition via `children` lets a Client Component wrap server-rendered content without turning it into a Client Component.
  5. In the composition pattern, a Client Component that accepts children is rendered by a ___ parent.