Client boundary and state
Scope: Next.js 16.x candidate, App Router, Cache Components disabled, TypeScript. Source walkthroughs and expected outcomes; no framework build or browser execution has been verified.
What you will learn
- Place use client at an interactive leaf
- Explain hydration
app/catalog/Quantity.tsx — complete interactive leaf
'use client';import { useState } from 'react';export default function Quantity({ title }: { title: string }) { const [count, setCount] = useState(1); return <section aria-label={`Quantity for ${title}`}> <button type="button" disabled={count === 1} onClick={() => setCount(n => Math.max(1, n - 1))}>Decrease</button> <p role="status">Quantity: {count}</p> <button type="button" disabled={count === 5} onClick={() => setCount(n => Math.min(5, n + 1))}>Increase</button> </section>;}Initial text is Quantity: 1. Increase changes it to 2; at 5 Increase is disabled. Decrease cannot go below 1. These are expected browser results, not observed tests.
How it works
The directive creates a boundary in the module dependency graph. Imports used from that entrypoint participate in the client graph; it is not necessary to repeat the directive in every imported child. On a first load, Next.js can prerender Client Components to HTML, then React connects event handlers during hydration. Browser-only operations must not be executed unconditionally during the initial render. In the quantity picker, functional state updates preserve the 1–5 limit and the disabled buttons communicate the bounds.
Server page fragment — composition
import Quantity from "../Quantity";export function BookActions({ title }: { title: string }) { return <Quantity title={title} />; }The server parent passes a title string while the leaf owns its count. No global client directive is required.
Common mistake
Do not assume use client means code can freely read window during render: initial HTML may be generated on the server. Avoid moving the entire root layout into the client graph merely to support one button.
Try it yourself
Predict the count after Increase, Increase, Decrease from 1. The expected result is 2. In a real browser verify button state, keyboard input and the status text.
Further reading
Next lesson: Passing data across boundaries.