Next.jsMedium

Can you import a Server Component inside a Client Component?

No, not directly. Anything imported by a "use client" module becomes client code, so an imported "Server Component" would run in the browser and lose server-only abilities, or fail if it uses server APIs. Instead, pass Server Components to Client Components as children or other props from a Server Component parent.

What goes wrong if you import it

  • An async component cannot render in the browser.
  • cookies(), database clients or fs break or leak into the bundle.
  • With import "server-only" in the module, the build fails, which is the desired guardrail.

The supported pattern

  1. A Server Component parent renders the Client Component.
  2. It passes the Server Component as JSX: <ClientShell><ServerList /></ClientShell> or <ClientShell sidebar={<ServerSidebar />} />.
  3. The Client Component renders {children} or {sidebar} wherever it wants.

The Server Component is rendered on the server first; the Client Component only receives its rendered output.

Why this works

The Client Component never imports the server module. React resolves the server part before sending the payload, so the client gets a ready-made element.

Calling server code from a Client Component

Client Components can import Server Functions from a "use server" file. That does not import server code; it imports a reference that makes a network call.

Interview summary

"Client cannot import server, but server can pass server into client through props."

Code example

tsx
1// app/page.tsx - Server Component composes both
2import { Modal } from "./modal"; // "use client"
3import { CartSummary } from "./cart-summary"; // async Server Component
4
5export default function Page() {
6 return (
7 <Modal title="Your cart">
8 <CartSummary /> {/* rendered on the server, passed as children */}
9 </Modal>
10 );
11}
12
13// app/modal.tsx
14// "use client";
15// import { useState } from "react";
16// // import { CartSummary } from "./cart-summary"; <- wrong: would become client code
17// export function Modal({ title, children }: { title: string; children: React.ReactNode }) {
18// const [open, setOpen] = useState(false);
19// return (
20// <>
21// <button onClick={() => setOpen(true)}>{title}</button>
22// {open && <dialog open>{children}<button onClick={() => setOpen(false)}>Close</button></dialog>}
23// </>
24// );
25// }

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app