In this lesson you will
- Explain where Server and Client Components run and what each can do
- Add "use client" only to the small parts that need interactivity
- Pass data from a server component to a client component with props
- Read the real build errors for useState, event handlers and server-only
Server Components are the default
Every component in the app folder is a Server Component unless you say otherwise. It runs on the server, either at build time or when a request arrives, and its result is sent to the browser as HTML plus a compact description of the tree that React calls the RSC Payload. The component code itself never reaches the browser. That is why the console.log in lesson 1 printed in the terminal and not in the browser console.
Running on the server gives Server Components real powers. They can be async and await data directly, query a database, and use API keys without exposing them, because none of that code is shipped. They also keep the JavaScript download small: a page made only of Server Components needs no component code in the browser at all. The trade-off is that they cannot be interactive. They have no state, no effects, no onClick, and no access to window or localStorage, because by the time the reader clicks, the server has finished.
Below we gave the shops page a like button that uses useState, without marking anything as a client component. The build refused it and printed exactly where the problem is and how to fix it.
import { useState } from "react";
export default function LikeButton() {
const [likes, setLikes] = useState(0);
return <button onClick={() => setLikes(likes + 1)}>Like ({likes})</button>;
}import Link from "next/link";
import LikeButton from "@/components/LikeButton";
export default function ShopsPage() {
console.log("Rendering ShopsPage");
return (
<>
<h1>Shops by city</h1>
<ul>
<li><Link href="/shops/dubai">Dubai</Link></li>
<li><Link href="/shops/sharjah">Sharjah</Link></li>
</ul>
<LikeButton />
</>
);
}> Build error occurred
Error: Turbopack build failed with 1 error:
./components/LikeButton.tsx:1:10
Error: You're importing a module that depends on `useState` into a React Server Component module. This API is only available in Client Components. To fix, mark the file (or its parent) with the `"use client"` directive.
Learn more: https://nextjs.org/docs/app/api-reference/directives/use-client
> 1 | import { useState } from "react";
| ^^^^^^^^
2 |
3 | export default function LikeButton() {
4 | const [likes, setLikes] = useState(0);
Ecmascript file had an error
Import trace:
Server Component:
./components/LikeButton.tsx
./app/shops/page.tsxRead the import trace from the bottom up: the server page imported the button, so the button was treated as a server component too.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
Do client components still render on the server in Next.js?
Yes, for the first page load. Next.js renders them to HTML on the server so the page shows straight away, then their JavaScript loads and React makes them interactive. "Client" means they also run in the browser, not that they only run there.
Can I use React context in a Next.js server component?
No. Context needs a client component. Create a small provider file with "use client" that takes children, and render it inside your layout. Server components can sit inside it as children.
Where does console.log from a server component appear?
In the terminal that runs next dev, next build or next start, not in the browser console. Logs from client components appear in the browser console, as in plain React.
Do I add "use client" to every component inside a client component?
No. The directive marks a boundary. Everything that a client file imports is already client code. Add it only to the files that server components import directly.
Are environment variables safe in Next.js client components?
Only variables whose names start with NEXT_PUBLIC_ reach the browser, and they are public: anyone can read them in your JavaScript. Keep real secrets in variables without that prefix and use them only in server code.
Sources
Facts in this lesson were checked against these official pages on 25 November 2026.
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.