Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 06 / Topic 5 of 6

Composition: Server Components passed as children into Client Components

ExpandablePanel needs client state (is it open?) but the content it reveals — ProductSpecs, a product's category and city — has no interactivity of its own and has no reason to ship any JavaScript. The installed package's own docs name this exact shape: "You can pass Server Components as a prop to a Client Component... A common pattern is to use children to create a slot." ExpandablePanel is built as that slot:

// harness/app/store/_components/ExpandablePanel.tsx
"use client";
import { useState } from "react";

export function ExpandablePanel({ title, children }: { title: string; children: React.ReactNode }) {
  const [open, setOpen] = useState(false);
  return (
    <div>
      <button type="button" onClick={() => setOpen((value) => !value)} aria-expanded={open}>
        {open ? "Hide" : "Show"} {title}
      </button>
      {open && <div>{children}</div>}
    </div>
  );
}

ExpandablePanel.tsx never imports ProductSpecs anywhere — it only ever renders children, whatever that turns out to be. The page wires the two together:

<ExpandablePanel title="product details">
  <ProductSpecs product={product} />
</ExpandablePanel>

<ProductSpecs product={product} /> is evaluated on the server, inside QuickAddPage, before it is ever handed to ExpandablePanel — by the time ExpandablePanel (a Client Component) sees it, it is already-rendered output, not a component reference it could choose to call differently. This is the only legal way to get server-rendered content "inside" a Client Component's boundary; a "use client" file is never allowed to import a Server Component directly.

Proof that the server-rendered content really is already present, not fetched lazily when the panel opens: the collapsed page's own HTML already embeds ProductSpecs's rendered output inside the React Server Component payload, before any click happens. Grepping the raw HTML response for one of ProductSpecs's values (home-decor, product p1's category) finds it, even though the panel renders collapsed by default:

$ npm run build
$ grep -o 'home-decor' .next/server/app/store/quick-add/p1.html
home-decor

Reading the few hundred characters around that match in the raw payload shows exactly what was found — a serialized React element tree, not visible text. (The flight payload sits inside a <script> tag as an escaped JS string, so every " in it is actually \" in the raw bytes — shown faithfully below, not cleaned up, because that escaping is itself the reason a grep pattern with literal " characters would miss this match.)

\"$\",\"dd\",null,{\"style\":{\"margin\":\"0 0 0.5rem\"},\"children\":\"home-decor\"}]

That confirms the mechanism directly: ProductSpecs ran once, on the server, as part of the same render pass that produced the rest of the page, and its output rode along inside ExpandablePanel's children prop in the very first response — ExpandablePanel only decides, client-side, whether to display it, never whether to fetch it.

Full files: examples/02-expandable-panel.tsx, examples/03-product-specs.tsx.

Done here? The practice set for Part 6 — including the solutions — is on the part page.

Search the guide