The client boundary
Server Components are the default, so you need a way to opt a component into the
client. Add the 'use client' directive as the very first line of the file,
before any imports:
'use client';
import { useState } from 'react';
export function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
'use client' marks the boundary between server and client. It applies to
the file and to every module that file imports: once you cross the directive,
everything below it is part of the client bundle. You do not repeat the
directive in child components — they inherit it.
Client Components can use state, effects, event handlers, and browser APIs. They
are still rendered to HTML on the server for the initial page load, then
hydrated in the browser so they become interactive. So 'use client' means
"this runs on the client", not "this is never rendered on the server".
The directive must be the first statement; placed anywhere else it is ignored. And because everything under the boundary ships to the browser, keep Client Components at the leaves of your tree, wrapping as little as possible.
You are about to answer a short quiz on 'use client'.