Server Components vs Client Components and use client

Next.js Course: Beginner to Advanced with Examples · Components and Layouts · 6 min read · Updated

Kitchen and dining room

  1. A Server Component runs only on the server. It can read data and keep secrets.
  2. It sends ready HTML, not its own code, to the browser.
  3. A Client Component marked use client is sent with its code, so it can react to the visitor.
  4. The visitor clicks and types, and only the client parts need to answer.

This is the most important idea in the App Router, so we will go slowly. Picture a restaurant again. The kitchen is the server and the dining room is the browser. Some work is best done in the kitchen: chopping, cooking, reading the recipe book. Some work must happen at the table: adding salt, pouring your own drink. Next.js lets you choose, component by component, where the work happens. A component that works in the kitchen is a Server Component. A component that works at the table is a Client Component.

Server Components (the default)

In the App Router every component is a Server Component unless you say otherwise. A Server Component runs only on the server. Its code is never sent to the browser. Only the finished result is sent, as ready HTML. This gives you real benefits.

  • Less JavaScript in the browser. The code of a Server Component, and the large libraries it uses, stay on the server. Pages load faster.
  • Direct access to data. A Server Component can be async, so you can write await and read a database or call an API right inside it.
  • Secrets stay safe. API keys and database passwords used in a Server Component never reach the visitor.
  • Better for search. The content is in the HTML from the start.

But a Server Component cannot do some things, because it never runs in the browser. It cannot use state or effects, and it cannot react to clicks.

  • No useState, useEffect or other hooks that depend on the browser.
  • No event handlers such as onClick or onChange.
  • No browser-only objects such as window or localStorage.

Client Components

A Client Component is a normal React component, the kind you already know. You make one by putting the line use client at the very top of the file, before the imports. That line says: this file, and what it imports, belongs to the browser side. Client Components can use state, effects and event handlers.

"use client";

import { useState } from "react";

export default function LikeButton({ start }: { start: number }) {
  const [likes, setLikes] = useState(start);
  return <button onClick={() => setLikes(likes + 1)}>Likes: {likes}</button>;
}

This is a Client Component. It remembers a number in state and adds one on each click. Next.js still prepares its first HTML on the server. After that, React in the browser makes the button work. This step is called hydration.

Using both together

Real pages mix the two. The rule of thumb is simple: keep components on the server by default, and move only the interactive pieces to the client. Here a Server Component draws an article and places the Client Component inside it.

import LikeButton from "./LikeButton";

// A Server Component: it can be async and read data directly.
export default async function ArticleCard() {
  const article = { title: "Server and client together", likes: 3 };
  return (
    <article>
      <h2>{article.title}</h2>
      <LikeButton start={article.likes} />
    </article>
  );
}

We built this page and ran it. The server drew the heading and the starting number of likes, and the button became interactive in the browser. Notice that ArticleCard did not need the use client line. A Server Component can import and render a Client Component.

What is the boundary?

The line use client creates a boundary. Everything in that file, and every file it imports, is sent to the browser. So place the line as low in the tree as you can. If you put it at the top of a big layout, all of its children get shipped to the browser, and you lose the benefit. Make a small button component client side instead.

Can a Client Component contain a Server Component?

Not by importing it. If a Client Component imports another component, that one becomes part of the client bundle. But a Client Component can receive a Server Component as children. The parent, which is a Server Component, passes it in. This pattern lets you wrap server content in an interactive shell, such as a modal or a tabs component.

// Modal.tsx  (Client Component)
"use client";
import { useState } from "react";

export default function Modal({ children }: { children: React.ReactNode }) {
  const [open, setOpen] = useState(false);
  return (
    <>
      <button onClick={() => setOpen(true)}>Open</button>
      {open && <div role="dialog">{children}</div>}
    </>
  );
}

// page.tsx  (Server Component)
import Modal from "./Modal";
import ServerData from "./ServerData";

export default function Page() {
  return (
    <Modal>
      <ServerData />
    </Modal>
  );
}

We built this pattern in the lab. The page rendered the Open button, and the server data was part of the page. We did not click the button in a browser. Adjust the names to your own files.

A quick way to choose

  • Need state, effects, click handlers or browser APIs? Use a Client Component.
  • Only showing data, with no interaction? Use a Server Component.
  • Need a secret key or a database? Server Component.
  • Using a library that needs the browser? Wrap it in a small Client Component.

Your task

  1. Make a LikeButton Client Component with useState, as shown.
  2. Place it inside a Server Component that is async and shows a title.
  3. Try removing use client from LikeButton. Read the error message that the build gives you.
  4. Move use client as low as you can in one of your own pages.

How to check it worked

  • The button works and the page title is drawn by the server.
  • The error without use client says that useState needs a Client Component.
  • You can explain in one sentence why use client should sit low in the tree.

Frequently asked questions

Are Server Components rendered only once?

They run on the server for each build or request, depending on how the page is prepared. They do not re-render in the browser when state changes. Client Components re-render in the browser like normal React.

Do Client Components also render on the server?

Yes. Next.js prepares their first HTML on the server, then React in the browser makes them interactive. That is why they still show quickly.

Where do I put use client?

At the very top of the file, before the imports. Put it in the smallest file that needs state, effects or events.