Server & Client Components
Next.js server client components: when to use a Server Component, when to add use client, fetching data on the server and keeping interactive parts in Client Components.
On this page
1. What are Server and Client Components?
In the App Router, every component is a Server Component by default. When you need useState, onClick or a browser API, you add "use client" at the top of the file.
2. Why does it matter?
| Everything on the client | Server + Client mix |
|---|---|
| JS for the whole UI goes to the browser | JS only for the interactive parts |
| API route + useEffect to get data | Direct await in a Server Component |
| You have to worry about hiding API keys | Secrets stay on the server |
| A loading spinner first | HTML arrives with the data |
| Large bundle, hard on slow phones | Small bundle, fast load |
3. Where is it used?
| Task | Which component |
|---|---|
| Fetching from a database or API | Server |
| Secrets, API keys, tokens | Server |
| Large libraries (markdown parser, date formatting) | Server |
onClick, onChange, live form state | Client |
useState, useEffect, useContext | Client |
window, localStorage, geolocation | Client |
4. How does it work?
"use client" is a boundary. The file that contains it, and all the components that file imports, go into the client bundle.
Remember two rules:
- A Server Component can import a Client Component.
- A Client Component cannot import a Server Component, but it can receive one through
childrenor props.
Props that go from the Server to the Client must be serializable: strings, numbers, plain objects, arrays and Promises. Functions cannot be passed (except Server Actions).
5. Code example
A Server Component page that uses a Client Component
import { LikeButton } from "./like-button";
async function getPost(id: string) {
const res = await fetch(`https://api.example.com/posts/${id}`);
return res.json();
}
export default async function PostPage({ params }: PageProps<"/posts/[id]">) {
const { id } = await params;
const post = await getPost(id);
return (
<article>
<h1>{post.title}</h1>
<p>{post.body}</p>
<LikeButton initialLikes={post.likes} />
</article>
);
}Client Component
"use client";
import { useState } from "react";
export function LikeButton({ initialLikes }: { initialLikes: number }) {
const [likes, setLikes] = useState(initialLikes);
return <button onClick={() => setLikes(likes + 1)}>Like {likes}</button>;
}Protecting server-only code
import "server-only";
export async function getSecretStats() {
const res = await fetch("https://api.example.com/stats", {
headers: { Authorization: `Bearer ${process.env.STATS_API_KEY}` },
});
return res.json();
}How does the code work?
| Part | Meaning |
|---|---|
PostPage | A Server Component, async, fetches data directly |
LikeButton | A Client Component, uses useState and onClick |
initialLikes | A serializable prop passed from the server to the client |
"use client" | A boundary, everything below this file goes into the client bundle |
import "server-only" | Causes a build error if it is imported into the client by mistake |
Interview tip
"Does use client mean the component does not render on the server?" No. Client Components also render on the server the first time to produce HTML, and then hydrate in the browser.
6. Common mistakes
Mistake: Adding use client at the top of a page
Why? The whole page and all its imports go into the client bundle, and you lose the benefits of the server.
Better: Move the interactive part into a small separate Client Component and keep the page a Server Component.
Mistake: Importing a Server Component into a Client Component
Why? As soon as it is imported, it becomes a client component too, and server-only code fails.
Better: Pass the Server Component into the Client Component through the children prop.
Mistake: Passing a function prop from the server
Why? Functions are not serializable, so Next.js throws an error.
Better: Pass data, or pass a Server Action for mutations.
Mistake: Using a secret env variable in a Client Component
Why? Without NEXT_PUBLIC it will be undefined, and adding NEXT_PUBLIC leaks the secret into the browser.
Better: Read secrets only in Server Components or server-only files.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | React Server Components, React state, Routing & Pages |
| Alongside | Data Fetching, Environment Variables |
| After | Rendering Strategies, Streaming & Suspense, Server Actions |
Final quick revision
| Point | Detail |
|---|---|
| Default | Server Component |
| When to use Client | State, events, hooks, browser APIs |
| Boundary | "use client" at the top of the file |
| Server inside client | Through children or props |
| Props | Must be serializable |
| Protection | import "server-only" |