Skip to content
RenderingChapter 11 / 34Intermediate4 min read

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 clientServer + Client mix
JS for the whole UI goes to the browserJS only for the interactive parts
API route + useEffect to get dataDirect await in a Server Component
You have to worry about hiding API keysSecrets stay on the server
A loading spinner firstHTML arrives with the data
Large bundle, hard on slow phonesSmall bundle, fast load

3. Where is it used?

TaskWhich component
Fetching from a database or APIServer
Secrets, API keys, tokensServer
Large libraries (markdown parser, date formatting)Server
onClick, onChange, live form stateClient
useState, useEffect, useContextClient
window, localStorage, geolocationClient

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.

The rendering journey
Server Components render on the server
RSC payload + HTML are created
HTML goes to the browser (visible immediately)
JS for Client Components loads
Hydration, buttons become interactive

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 children or 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

tsx
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

tsx
"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

ts
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?

PartMeaning
PostPageA Server Component, async, fetches data directly
LikeButtonA Client Component, uses useState and onClick
initialLikesA 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

Final quick revision

PointDetail
DefaultServer Component
When to use ClientState, events, hooks, browser APIs
Boundary"use client" at the top of the file
Server inside clientThrough children or props
PropsMust be serializable
Protectionimport "server-only"