Sub topic 3
The "use client" boundary
This chapter assumes you know React components, props, useState and useEffect, and async/await. It also assumes you know that Server Components render only on the server and Client Components can use state and effects. Here you will learn what the "use client" directive does, where to put it, and what goes wrong when it is misplaced.
What the directive does
"use client" is a directive at the top of a module. It marks that module as an entry point into client code. The module itself and everything it imports become part of the client bundle.
// 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 directive must be the first statement in the file, before any imports. It applies to a module, not to a single component. Every export of that file is a Client Component.
The boundary is on the import graph
Components do not carry a "client" or "server" label based on where they appear in the JSX tree. The label comes from the module graph. A module is a Server Component unless it has "use client" or is imported by a module that does.
Only the entry file of a client subtree needs the directive. Files imported from Counter.js are already client code, so they do not need their own "use client".
What each side can do
| Capability | Server Component | Client Component |
|---|---|---|
| `async`/`await` in the component | Yes | No |
| Read a database or secrets directly | Yes | No |
| `useState`, `useEffect` | No | Yes |
| Event handlers such as `onClick` | No | Yes |
| Browser APIs such as `window` | No | Yes |
| Code sent to the browser | None for the component | Included in the bundle |
A Client Component is still rendered on the server for the first HTML. That is server-side rendering, covered in a later chapter. It is separate from being a Server Component.
Where to place the boundary
Place the directive as deep in the tree as you can. Every module behind the boundary ships to the browser, so a boundary at the top of a page pulls the whole page into the bundle.
Compare two designs for a page that shows a long article and a like button:
- Put
"use client"on the page. The article, its Markdown parser, and the button all ship to the browser. - Keep the page as a Server Component and put
"use client"only onLikeButton.js. Only the button code ships.
Option 2 is usually correct. Extract the interactive piece into a small leaf component and import it from the server side.
// app/LikeButton.js
'use client';
import { useState } from 'react';
export default function LikeButton() {
const [liked, setLiked] = useState(false);
return (
<button onClick={() => setLiked(!liked)}>
{liked ? 'Liked' : 'Like'}
</button>
);
}// app/page.js (Server Component)
import LikeButton from './LikeButton';
import { renderMarkdown } from './markdown'; // stays on the server
export default async function Page() {
const html = await renderMarkdown('# Hello');
return (
<article>
<div dangerouslySetInnerHTML={{ __html: html }} />
<LikeButton />
</article>
);
}Common errors
- Using a hook in a Server Component.
useStatethere fails at build or render time. Move the stateful code into a file with"use client". - Putting the directive after an import. It is ignored when it is not first, so the module stays a server module.
- Importing a server-only module into client code. A client file that imports your database module pulls it into the bundle. Mark server-only modules so the build fails instead, for example with the
server-onlypackage. - Adding
"use client"to every file. This works, but it removes the benefit of Server Components.
Exercise
A page imports Sidebar.js, which has "use client". Sidebar.js imports Menu.js, which has no directive. Is Menu.js a Server Component or a Client Component, and why?
Show answer
Menu.js is a Client Component. The boundary follows the import graph, so any module imported by a client module becomes part of the client bundle, even without its own directive.