Nextjs Fundamentals · Server & Client Components

The client boundary

`'use client'`

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'.

  1. The `'use client'` directive marks a file and its imports as Client Components.
  2. `'use client'` must appear at the ___ of the file, before any imports.
  3. Every component imported by a Client Component also needs its own `'use client'` directive.
  4. Client Components are prerendered to HTML on the server for the initial page load, then hydrated in the browser.
  5. To keep the client bundle small, place Client Components as ___ nodes in the component tree.
  6. A Client Component can use state, effects, and event handlers.