Composing Server and Client Components

Intermediate · 8 min

Which nestings of server and client components work, why importing a Server Component into a Client Component fails, and the children slot that fixes it.

Before thisThe use client boundary

Codevolution · 8 min

Composing Server and Client Components

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodevolutionReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A Server Component can render other Server Components and Client Components, and a Client Component can render other Client Components.
  • A Client Component cannot import a Server Component: the import makes it a client module, so it is bundled for the browser and loses its server-only ability.
  • A Client Component can still show a Server Component if the parent passes it in as `children` or another prop, because the server renders it ahead of time.
  • Props crossing from server to client must be serializable, and a Server Component's output can be passed through as a slot.

The question

Once you have two kinds of component, you will nest them. A <Modal> needs state to open and close, so it is a Client Component. Inside it you want a <Cart> that fetches from your database, so it is a Server Component. Can you put one inside the other? It depends on who writes the import.

Four nestings

Take each in turn.

Server inside Server. Fine. Components rendering components on the server is the default.

Client inside Client. Fine. Everything in the client module graph runs in the browser.

Client inside Server. This is the normal case: a page, a Server Component, renders <LikeButton />. On the server, Next.js emits a placeholder for the button in the RSC payload with a reference to its JavaScript. The browser hydrates it.

Server inside Client, by import. This is the one that breaks. Suppose modal.tsx has "use client" and writes import { Cart } from "./cart". That import pulls cart.tsx into the client module graph. It is now bundled for the browser, and if Cart reads a database or a secret, it fails there. A component’s type is decided by where it is imported from, not by what its file looks like.

The fix: pass it in

You do not need to import Cart into Modal. Instead, have a parent that is a Server Component render both and hand Cart to Modal as children:

// components/modal.tsx
"use client";
import { useState } from "react";

export function Modal({ children }: { children: React.ReactNode }) {
  const [open, setOpen] = useState(false);
  return (
    <>
      <button onClick={() => setOpen(!open)}>Toggle</button>
      {open && <div>{children}</div>}
    </>
  );
}
// app/page.tsx  (Server Component)
import { Modal } from "@/components/modal";
import { Cart } from "@/components/cart";

export default function Page() {
  return (
    <Modal>
      <Cart />
    </Modal>
  );
}

Page runs on the server and renders <Cart /> there, ahead of time. Modal receives the result as children and decides where to put it. It never imports Cart, so Cart stays a Server Component. The RSC payload carries the rendered Cart plus a placeholder for Modal. The client component gets a slot, and the server fills it.

I built this exact pair on Next.js 16 and it compiled and prerendered cleanly.

Use children for one slot; any prop works for more, such as header={<Header />} and sidebar={<Sidebar />}. This is the same composition idea as in React generally: a component that does not care what its children are can host anything, including server-rendered output. See lifting state up and composition.

Why wrapper components are the usual workaround

A common practical situation: a Client Component needs a server-rendered thing inside it, but the nesting is awkward. The cure is rarely to make the child a client component. Instead, restructure so a Server Component sits above both and composes them as slots. Where you do convert a component to a client one, keep it small, as in the use client boundary.

Serialization

Props that cross from server to client travel in the payload, so they must be serializable. A Server Component cannot pass an inline event handler to a Client Component, since a function from the server cannot be sent. Pass data and let the Client Component define its own handlers. You can also stream a promise from a Server Component into a Client Component and read it with React’s use.

What the video covers

Codevolution’s chapters set up a project structure, create Server and Client Components, then walk the four cases in turn: a Server Component in a Server Component, a Client in a Client, a Client in a Server, and a Server in a Client, finishing with the workaround for the unsupported pattern. That maps directly onto this lesson’s structure. It was recorded for Next.js 15, and the rules are unchanged in 16. ByteGrad’s shorter video on the same trap is linked under deeper.

Where this fits

Composition is the tool for mixing the two kinds. Next, environment variables and server-only code covers keeping secrets on the server side of the boundary.

Check yourself

  1. 01Why does importing a Server Component into a Client Component not keep it a Server Component?

    An import inside a client file pulls the module into the client module graph, so it is bundled for the browser and treated as client code. Anything it does that only works on the server, such as reading a database, fails.

  2. 02How can a `<Modal>` Client Component display a Server Component `<Cart>` that fetches data?

    Render `<Cart />` in a Server Component parent and pass it as the `children` of `<Modal>`. The server renders `Cart` ahead of time, and `Modal` receives the result as a slot it places in its output without importing `Cart`.

  3. 03Is it a problem to render a Client Component inside a Server Component?

    No, this is the normal pattern. The Server Component renders on the server and leaves a placeholder in the RSC payload for the Client Component, which hydrates in the browser.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark