Server Components — React

A Server Component is a React component that runs only on the server. It may use async/await, read the database directly and use secrets (private data). But…

A product page has just one button you can click. So why should visitors download the code for every other component, including a 36 KB markdown library (it turns markdown text into HTML) that's only used to show the description?

Server Components are the answer. A Server Component is a React component that runs only on the server. Its render result is sent to the browser, but its code never is. A component that still runs in the browser, for the interactive parts (the ones you can click or type in), is called a Client Component. React 19 provides both, but you use them through a framework that runs the server side, most often Next.js with the App Router (the way Next.js arranges pages in the app folder). Imagine a restaurant. The kitchen cooks, and what reaches your table is the finished dish, not the recipe and the stove. Tools come to the table only for work you really do there, such as a bottle of chilli sauce, because you pour it yourself. The kitchen may also open the pantry, which guests never see. In React, the kitchen is the server and your table is the browser. The finished dish is the render result, and the recipe and the stove are the component's code. The chilli sauce bottle is a Client Component: its code is sent along because it's used in the browser. The pantry is the database and the secret keys.

Server Component: the default, and it may be async

This is a product page in Next.js. ProductPage is a Server Component:

// app/product/page.tsx: a Server Component (no "use client")
import { db } from "@/lib/db" // an example database library
import Description from "./Description"
import Counter from "./Counter"

export default async function ProductPage() {
  const product = await db.product.findFirst() // straight to the database

  return (
    <main>
      <h1>{product.name}</h1>
      <p>${product.price}</p>
      <Description text={product.description} />
      <Counter />
    </main>
  )
}

Client Component: "use client" on the first line

The +1 button needs state and a click, so Counter is made a Client Component:

// app/product/Counter.tsx: a Client Component
"use client"

import { useState } from "react"

export default function Counter() {
  const [count, setCount] = useState(0)

  return (
    <div>
      <p id="count">{count}</p>
      <button id="increment" onClick={() => setCount(count + 1)}>+1</button>
    </div>
  )
}

What gets sent to the browser

With that split between ProductPage and Counter, the browser receives three things:

  1. HTML for the finished page: the name, the price, the description, the button, the number 0.
  2. The RSC payload (RSC stands for React Server Components): the render result of the Server Components in React's format, plus a note saying "a Counter goes here, these are its props".
  3. JavaScript: the React and Next.js runtime (the code that runs the page in the browser), then only Counter's code.

ProductPage, Description and its markdown library stay on the server. These are example numbers, the same as in the Lab (sizes after gzip compression):

"use client" placed on JavaScript sent
Counter only runtime 100 KB + Counter 1 KB = 101 KB
Description and Counter 100 + 1 + Description 1 + markdown library 36 = 138 KB
ProductPage (the very top) fails: an async component and database access can't run in the browser

Description isn't interactive. If Description becomes a Client Component, every visitor has to download and run 37 KB more, with nothing to click.

Putting them together: server outside, client as small as possible

⚠️ Drawing the server/client line in the wrong place

TL;DR: A Server Component is a React component that runs only on the server. It may use async/await, read the database directly and use secrets (private data). But it can't use state, effects, event handlers or browser APIs (such as window). Only its render result is sent to the browser, not its code. A Client Component is marked with "use client" at the very top of the file. Its code is sent to the browser so that useState and onClick work. In Next.js (a React framework) with the App Router (the way Next.js arranges pages), components are Server Components by default. Put "use client" as low as you can in the component tree, and pass a Client Component only props that are serialisable (can be turned into data to send). Then the JavaScript that visitors download stays small: 101 KB in this lesson's product page, not 138 KB.

⚡ Server Components: the default, and they may be async

Write:

// app/product/page.tsx: a Server Component (no "use client")
import { db } from "@/lib/db" // an example database library
import Description from "./Description"
import Counter from "./Counter"

export default async function ProductPage() {
  const product = await db.product.findFirst() // straight to the database

  return (
    <main>
      <h1>{product.name}</h1>
      <p>${product.price}</p>
      <Description text={product.description} />
      <Counter />
    </main>
  )
}

Meaning: A Server Component is a React component that runs only on the server. In Next.js (a React framework) with the App Router (the way Next.js arranges pages), a component without "use client" is a Server Component. ProductPage may use async/await and read the database (db) directly. Its render result is sent to the browser, but its code never is.

⚡ Client Components: "use client"

Write:

// app/product/Counter.tsx: a Client Component
"use client"

import { useState } from "react"

export default function Counter() {
  const [count, setCount] = useState(0)

  return (
    <div>
      <p id="count">{count}</p>
      <button id="increment" onClick={() => setCount(count + 1)}>+1</button>
    </div>
  )
}
Guess first: this code uses useState and onClick, so it needs "use client". What can a Server Component not do?

Use state and event handlers. On the server there's nothing to click.

Meaning: "use client" at the very top of the file makes that component a Client Component: its code is sent to and runs in the browser. So Counter may use useState and onClick.

⚡ Client as small as possible

Meaning: Put "use client" as low as you can in the component tree (the way components sit inside each other), only on the components that need interaction. In ProductPage above, only Counter's code is sent to the browser. ProductPage and Description stay on the server, because they have nothing to click.

What you will learn