0Pricing
Next.js 15 Fullstack (App Router + Server Actions) · Lesson

Composition Patterns for Server and Client Components

Learn how to compose Server and Client Components correctly, passing Server Components as children to avoid bloating the client bundle.

Composition Patterns for Server and Client Components is a free Next.js 15 Fullstack (App Router + Server Actions) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Next.js 15 Fullstack (App Router + Server Actions) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Composition Challenge

You often need interactivity (Client) wrapping data-heavy UI (Server). The trick is composing them so Server Components are not pulled into the client bundle.

The One-Way Rule

A Client Component cannot import a Server Component, because importing would force it to run on the client. But it can render one passed as children.

Passing as Children

Render a Server Component inside a Client Component by passing it through props from a parent Server Component.

export default function Page() {
  return (<ClientShell><ServerContent /></ClientShell>);
}

The Client Shell

The Client Component receives the already-rendered Server output as children and never needs to know how it was built.

"use client";
export default function ClientShell({ children }) {
  const [open, setOpen] = useState(false);
  return (<div>{children}<button onClick={() => setOpen(!open)}>Toggle</button></div>);
}

Why This Works

The Server Component renders on the server; only its output (serialized RSC payload) reaches the client. The Client Component just slots it in — no server code shipped.

Props Must Be Serializable

Props passed from Server to Client Components must be serializable. You cannot pass functions, class instances, or Dates without care — but you can pass plain data and JSX children.

Moving Client Boundaries Down

Keep "use client" as deep in the tree as possible. A small leaf Client Component keeps most of the tree server-rendered and the bundle small.

Context Providers

Client context providers wrap Server children fine: define the provider as a Client Component and pass server-rendered children through it.

"use client";
export function ThemeProvider({ children }) {
  return <Ctx.Provider value={theme}>{children}</Ctx.Provider>;
}

Avoiding Accidental Client Code

Importing a library that uses browser APIs into a Server Component breaks it. Isolate such code behind a Client Component boundary.

Interleaving Example

You can deeply interleave: Server layout, Client sidebar toggler, Server content, Client like-button — each boundary minimal and intentional.

Mental Model

Think of Client Components as holes in a server-rendered tree. Data flows down as props/children; interactivity lives only where needed.

Quick Check

How do you render a Server Component inside a Client Component?

Recap

You learned the key composition pattern: Client Components cannot import Server Components but can render them as children. Keep client boundaries small and deep, pass serializable props, and treat Client Components as interactive holes in a server-rendered tree.

Frequently asked questions

Is the “Composition Patterns for Server and Client Components” lesson free?

Yes — the full text of “Composition Patterns for Server and Client Components” is free to read here on the web, and the Next.js 15 Fullstack (App Router + Server Actions) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Next.js 15 Fullstack (App Router + Server Actions) course, upgrade to CoddyKit PRO.

What will I learn in “Composition Patterns for Server and Client Components”?

Learn how to compose Server and Client Components correctly, passing Server Components as children to avoid bloating the client bundle. You practise Next.js 15 Fullstack (App Router + Server Actions) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Next.js 15 Fullstack (App Router + Server Actions)?

No prior experience is required. Next.js 15 Fullstack (App Router + Server Actions) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Composition Patterns for Server and Client Components” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Next.js 15 Fullstack (App Router + Server Actions) lesson?

Yes. Every Next.js 15 Fullstack (App Router + Server Actions) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding RSC & RCC
  2. Data Fetching in RSC
  3. Interactivity with RCC
  4. Composition Patterns for Server and Client Components
← Back to Next.js 15 Fullstack (App Router + Server Actions)