4. Client Components & Interactivity
Client Components & Interactivity
Server Components can't click, type, or remember state — they render once and stop. The moment your UI needs to react to the user, you need a Client Component.
Opting Into the Client
Add <strong><code>use client as the very first line of a file, and every component in that file — plus everything it imports — becomes part of the client bundle, hydrated and interactive in the browser.
// components/Counter.tsx 'use client'; import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)}> Clicked {count} times </button> ); }
Composing Server and Client Together
You don't convert whole pages to Client Components — you import a small interactive piece into an otherwise server-rendered page. The server still handles data fetching; the client piece only handles interactivity.
// app/page.tsx (Server Component, no directive) import Counter from '@/components/Counter'; export default function HomePage() { return ( <div> <h1>Welcome</h1> <Counter /> </div> ); }
The heading renders on the server; only Counter ships JavaScript to the browser and hydrates.
The One-Way Boundary
A Server Component can import and render a Client Component. The reverse isn"t true directly — a Client Component can"t import a Server Component, because by the time it's running in the browser, the server is no longer part of the picture. To mix them, pass the Server Component in as children instead.
<strong><code>use client only on the specific component that needs state, effects, or browser events — push it as far down the tree as possible.
// ServerComponent.tsx import ClientComponent from './ClientComponent'; export default function ServerComponent() { // Server components can import and pass data to Client components return <ClientComponent data="Server Data" />; }
Now that you can choose where each piece of your UI runs, the next few modules build out routing itself — starting with dynamic segments and route groups.
Deep Dive Reference: Client Components
Client Components aren't entirely un-rendered by the server. Here are the explicit rules and behaviors:
The "use client" Directive
The 'use client' directive does not mean the component only runs on the client. It means it is part of the client bundle and can use client-side React features. It is actually pre-rendered on the server (SSR) into static HTML for the initial page load, and then hydrated on the client.
Passing Server Components as Children
You cannot import a Server Component directly into a Client Component. However, you can pass a Server Component as a child or prop to a Client Component.
// Supported Pattern: export default function ClientWrapper({ children }) { return <div className="interactive">{children}</div>; } // Usage in a Server Component: // <ClientWrapper><ServerComponent /></ClientWrapper>
Rules of Hydration
If the HTML generated on the server doesn't match the HTML generated on the first client render, a Hydration Error occurs. Avoid using Math.random() or new Date() directly in the initial render output.
Knowledge Check
Ready to test your understanding of 4. Client Components & Interactivity?