Sub topic 9
Composing Server and Client Components
This chapter assumes you know props, children, useState, and the idea of a "use client" boundary from earlier chapters. It covers how to combine the two kinds of components, and why one pattern that looks like it should fail works well.
The import rule
A Server Component can import and render a Client Component. The reverse does not work the same way. When a module marked "use client" imports another module, that module joins the client bundle. If it is a Server Component, it stops being one. It would run in the browser, where it cannot use server-only resources such as a database.
So the question is not "who is rendered inside whom in the JSX." The question is "who imports whom."
Passing Server Components as children
You can still place server-rendered output inside a Client Component. The Server Component that owns both of them creates the elements, then passes one as a prop.
// app/Modal.js
'use client';
import { useState } from 'react';
export default function Modal({ children }) {
const [open, setOpen] = useState(false);
return (
<>
<button onClick={() => setOpen(!open)}>Toggle</button>
{open && <div className="modal">{children}</div>}
</>
);
}
// app/page.js (Server Component)
import Modal from './Modal';
import Comments from './Comments'; // a Server Component
export default function Page() {
return (
<Modal>
<Comments />
</Modal>
);
}Page renders on the server. Comments is rendered there too, because Page is the one that creates it. Modal never imports Comments. It only receives the finished output as children and decides where to place it.
What the payload looks like
The payload holds a client reference for Modal. Its children prop holds the already-rendered result of Comments. A simplified sketch:
1:I["app/Modal.js",["chunk-modal.js"],"default"]
0:["$","$L1",null,{"children":["$","section",null,{"children":"First comment"}]}]The browser never receives the code for Comments. It receives only its output, as plain elements inside the children prop.
The same idea with other props
children is not special. Any prop can carry server-rendered elements, so you can use named slots.
// app/Layout.js
'use client';
export default function Layout({ sidebar, content }) {
return (
<div className="layout">
<aside>{sidebar}</aside>
<div>{content}</div>
</div>
);
}A Server Component then passes <Layout sidebar={<Nav />} content={<Feed />} />. Both slots arrive as rendered output. This works because elements are serializable, as the previous chapter on props described. Functions and class instances are not.
Common mistakes
- Importing a Server Component inside a
"use client"file and expecting it to stay on the server. It joins the client bundle instead. - Marking a large parent as
"use client"for oneuseState. Everything it imports then moves to the client. Extract the interactive part into a small component. - Passing a function from a Server Component as a prop to a Client Component. Functions cannot be serialized, except Server Functions.
Exercise
A file marked "use client" contains import Comments from './Comments', where Comments is an async Server Component that queries a database. Why is this a problem, and how do you fix it?
Show answer
Importing Comments from a client module puts it in the client bundle, so it can no longer run only on the server and cannot use server-only data access. Fix it by having a Server Component, such as the page, import both the Client Component and Comments, then render <ClientComponent><Comments /></ClientComponent>. The Client Component should render children.