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>
)
}
- In the Next.js App Router, every component in the
appfolder is a Server Component, unless its file is marked"use client". Other frameworks outside Next.js that support Server Components: RedwoodSDK, theRSCvariant innpm create vite@latest, or React Router (its support is still experimental). async functionandawait: a Server Component may wait for data before it renders. For this data, nouseEffector loading state is needed.db.product.findFirst()runs on the server. So this code may use a database connection and secrets (private data, such as the database password) without leaking them to the browser. Secrets are usually read from environment variables (settings kept outside the code), such asprocess.env.DATABASE_URL.- What a Server Component can't use:
useState,useEffect, event handlers such asonClick, and browser APIs such aswindoworlocalStorage. There's no browser on the server, and this component's code never reaches the browser.
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>
)
}
"use client"goes at the very top, before the imports. A comment may sit above it, like the file name in this example. The"use client"line is the boundary between server and client: this file and everything it imports get sent to the browser.- Here
useStateandonClickmay be used. - A Client Component is still rendered to HTML on the server for the first view. In the browser, React then "brings that HTML to life" by attaching its event handlers. This is called hydration. So "client" doesn't mean "only in the browser": its code is also sent to and runs in the browser.
What gets sent to the browser
With that split between ProductPage and Counter, the browser receives three things:
- HTML for the finished page: the name, the price, the description, the button, the number 0.
- 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".
- 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
- A Server Component may render a Client Component and pass it props. Those props must be serialisable, meaning they can be turned into data to send. Examples are text, numbers, booleans, arrays, plain objects, and also
Date,MapandSet. Ordinary functions can't. - A Client Component can't import a Server Component, because whatever a
"use client"file imports becomes client too. If you need one, the Server Component above can pass that Server Component to the Client Component aschildren. - Put
"use client"as low as you can in the component tree, only on the components that need interaction.
⚠️ Drawing the server/client line in the wrong place
useStateoronClickin a Server Component. ForuseState, Next.js rejects it at build time and suggests adding"use client". ForonClick, the error appears when the page renders: a function can't be sent to the browser. Move the interactive part into its own small component marked"use client"."use client"at the top of the tree. Every child becomes client too, the JavaScript grows large, and anasynccomponent that fetches data directly can't be used any more.- Secrets in a Client Component. In Next.js, an environment variable whose name doesn't start with
NEXT_PUBLIC_isn't available in the browser: Next.js replaces it with an empty string. Fetch secret data in a Server Component. - The exercise. In the exercise, the editor runs plain React in the browser, without Next.js, so every component there is client. Write Counter as usual with
useState."use client"may be written or left out.
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.