Sub topic 1
Where Server and Client Components run
Server Functions only make sense once you know which code runs where. This chapter sets out that split. Later chapters build on it when they show how client code calls the server.
Prerequisites
This chapter assumes you have the following:
- Solid knowledge of React components, props, state, and hooks.
- Familiarity with
async/awaitand Promises in JavaScript. - A basic picture of the client-server HTTP request flow.
- Access to a framework or bundler that supports Server Components. Next.js App Router is one example. Plain React with no bundler support can't run the examples.
Two kinds of components
A React app that supports Server Components has two kinds of components. They differ in where their code can execute.
| Row | Server Component | Client Component |
|---|---|---|
| Default? | Yes | No, opt in with `'use client'` |
| Runs | Only on the server, at build time or per request | In the browser, and also on the server for the first HTML |
| Can be `async` | Yes | No |
| Can use state, effects, event handlers | No | Yes |
| Can read a database or secrets | Yes | No |
| Code sent to the browser | None | Yes, in the JavaScript bundle |
A Server Component renders once and produces output. It is never hydrated, so it has no state and no event handlers. Because its code never ships to the browser, it can safely import a database client or read a private environment variable.
A Client Component works like the components you already know. The server may render it to HTML for the first paint. After that, its code runs in the browser, where it hydrates and responds to clicks.
What "use client" marks
The 'use client' directive goes at the top of a file. It marks the file as the entry point into client code. That file, and every module it imports, goes into the browser bundle.
The directive marks a boundary in the module graph. You don't put it on every Client Component. You put it on the files that Server Components import directly.
// app/page.js (a Server Component)
import { db } from './db';
import { LikeButton } from './LikeButton';
export default async function Page() {
const notes = await db.notes.findAll();
return (
<ul>
{notes.map((note) => (
<li key={note.id}>
{note.title} <LikeButton noteId={note.id} />
</li>
))}
</ul>
);
}// app/LikeButton.js
'use client';
import { useState } from 'react';
export function LikeButton({ noteId }) {
const [liked, setLiked] = useState(false);
return (
<button onClick={() => setLiked(!liked)} aria-label={`Like note ${noteId}`}>
{liked ? '♥' : '♡'}
</button>
);
}Here db is a placeholder for your data layer. Page runs only on the server, and LikeButton is the only part of this tree that ships to the browser.
Crossing the boundary
Data moves from server to client through props. The props must be serializable, which means React can turn them into a stream and send them over the network. Strings, numbers, plain objects, arrays, and Promises are fine. A function defined in a Server Component can't be passed as a prop to a Client Component. The exception is a Server Function, which later chapters cover.
Server Components can also pass other Server Components as children to a Client Component. The Client Component renders them in place without ever receiving their code.
What "use server" does not mark
The names suggest a symmetry that doesn't exist. 'use server' does not mark a Server Component. Server Components need no directive, because they are the default.
The 'use server' directive marks functions the client can call, which are the Server Functions. 'use client' says "this code goes to the browser". 'use server' says "this function stays on the server, but client code may call it". The next chapter covers it in detail.
Exercise
A Server Component imports Chart.js, which starts with 'use client' and imports format.js, a file with no directive. Where does the code in format.js run?
Show answer
In the browser, and also on the server during the first render. A 'use client' file pulls every module it imports into the client bundle, so format.js becomes client code even without its own directive.