Sub topic 2
Server Components and Client Components: what each can do
This chapter assumes you know React components, props, JSX, useState and useEffect, and async/await. It covers what each kind of component can and cannot do. The "use client" boundary and the payload format come in later chapters.
Two kinds of components
In an RSC-enabled framework, every component belongs to one of two environments.
- A Server Component runs only on the server. Its code is never sent to the browser. Only its output is sent.
- A Client Component is the kind of component you already know. It runs in the browser, and it can also be rendered once on the server to produce initial HTML.
In frameworks such as Next.js App Router, components are Server Components by default. A file becomes client code only when it opts in. How it opts in is the subject of the next chapter.
What Server Components can do
A Server Component can be an async function, so it can await data directly while rendering. It can reach server-only resources such as databases, the file system, and secret keys.
// app/page.js (Server Component by default in an RSC framework)
import Counter from './Counter';
async function getPosts() {
const res = await fetch('https://example.com/api/posts');
return res.json();
}
export default async function Page() {
const posts = await getPosts();
return (
<main>
<h1>Posts</h1>
<ul>
{posts.map((p) => (
<li key={p.id}>{p.title}</li>
))}
</ul>
<Counter initial={posts.length} />
</main>
);
}Here Page fetches data, maps it to list items, and renders a Counter. The code for getPosts and the fetch call stays on the server. A library used only here, such as a Markdown parser, also adds nothing to the browser bundle.
A Server Component runs once per request or build. It has no lifecycle after that, so it cannot use:
- State or reducers:
useState,useReducer. - Effects:
useEffect,useLayoutEffect. - Event handlers such as
onClick. - Browser APIs such as
windoworlocalStorage.
What Client Components can do
A Client Component can hold state, run effects, respond to events, and use browser APIs. Its code is bundled and sent to the browser.
// app/Counter.js
'use client';
import { useState } from 'react';
export default function Counter({ initial }) {
const [count, setCount] = useState(initial);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
}The initial prop comes from the server. After it arrives, the button works like any React component. The click updates state in the browser, with no server round trip.
Side by side
| Capability | Server Component | Client Component |
|---|---|---|
| Runs in the browser | No | Yes |
| Can be `async` and `await` data | Yes | No |
| Reads database or secrets directly | Yes | No |
| `useState`, `useEffect` | No | Yes |
| Event handlers | No | Yes |
| Browser APIs | No | Yes |
| Code sent to the browser | No | Yes |
How they relate in one tree
The two kinds mix in a single tree. A Server Component can render a Client Component, as Page renders Counter. The server renders the server parts and leaves a placeholder where each Client Component goes.
The props passed to Counter travel from server to browser, so they must be serializable. Chapter 8 covers that rule.
Common mistakes
- Calling
useStatein a Server Component. The framework reports an error because hooks that need state or effects only work on the client. - Adding an
onClickto an element inside a Server Component. Move the interactive part into a small Client Component. - Making a whole page a Client Component just to get one button. That sends more code to the browser than necessary.
Exercise
A component fetches a list from a database and also has a "like" button that increments a counter on click. How would you split it between Server and Client Components?
Show answer
Fetch the list in a Server Component, which can be async and can reach the database. Put the like button in a separate Client Component that uses useState and onClick. The Server Component renders the Client Component and passes any initial values, such as the current like count, as serializable props.