Server Components vs. Client Components

Advanced · 53 min

What React Server Components run and can't run, the 'use client' boundary, and why a component is a Server Component by default.

Before thisCSR, SSR, and static rendering conceptsSuspense for data fetching

Theo - t3.gg · 53 min

Server Components vs. Client Components

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Theo - t3.ggReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A React Server Component (RSC) runs only on the server, never ships its own code to the browser, and its output is serialized data the client uses to build the page — not HTML in the SSR sense and not a bundle in the CSR sense.
  • Every component is a Server Component by default. You opt a file into being a Client Component by adding the `'use client'` directive at its top, not the other way around.
  • A Server Component can use `async`/`await` directly in its body and reach a database, filesystem, or private API with secrets that would be unsafe to expose, because none of that code or those secrets ever reaches the browser.
  • A Server Component cannot use state, effects, refs, or any hook that depends on them, and it cannot attach event handlers like `onClick`, because none of that has meaning without a running JavaScript environment in the browser.
  • The `'use client'` boundary is one-directional: a Client Component can be imported by a Server Component and rendered inside it, but a Client Component cannot import a Server Component's module and render it the same way — it receives Server Components only as children passed down to it.
  • This split between where code executes and what each side is allowed to do is a React rendering model, not a Next.js feature; Next.js's App Router is one implementation of it, and other frameworks implement the same RSC contract.

Two computers, one component tree

Up to now, every component in this roadmap has run somewhere specific and predictable: the browser, either immediately (CSR) or after hydration (SSR/SSG). React Server Components introduce a third possibility — a component that runs only on the server and never ships to the browser at all, not even as HTML to hydrate.

Think of it as two computers cooperating on one tree of components. The server computer has access to your database, your filesystem, your API keys, and nothing it does is visible to a user beyond whatever it decides to send. The client computer is the browser: it has access to the DOM, localStorage, clicks, and scroll position, but nothing it does is secret. A Server Component is code that only the server computer ever runs. A Client Component is code that ships to, and runs on, the client computer (after first rendering as part of the server’s initial response, same as any SSR page).

The default, and the opt-in

The detail that trips people up first: every component is a Server Component unless you say otherwise.

// ProductPrice.jsx — a Server Component, with no directive needed
import { db } from "./db";

export default async function ProductPrice({ productId }) {
  const product = await db.products.findById(productId);
  return <span>${product.price.toFixed(2)}</span>;
}

There is no “use server component” directive to add — the absence of any directive is the Server Component. To make a component run on the client instead, you add 'use client' as the very first line of the file:

// AddToCartButton.jsx — a Client Component
"use client";

import { useState } from "react";

export default function AddToCartButton({ productId }) {
  const [pending, setPending] = useState(false);

  return (
    <button
      disabled={pending}
      onClick={async () => {
        setPending(true);
        await fetch("/api/cart", { method: "POST", body: productId });
        setPending(false);
      }}
    >
      {pending ? "Adding…" : "Add to cart"}
    </button>
  );
}

'use client' marks the boundary for that file and everything it imports: once you cross into a Client Component, everything beneath it in that branch of the tree ships to the browser. You compose the two freely:

// ProductPage.jsx — a Server Component
import ProductPrice from "./ProductPrice";
import AddToCartButton from "./AddToCartButton";

export default async function ProductPage({ productId }) {
  const product = await db.products.findById(productId);
  return (
    <div>
      <h1>{product.name}</h1>
      <ProductPrice productId={productId} />
      <AddToCartButton productId={productId} />
    </div>
  );
}

ProductPage and ProductPrice run only on the server and can both query db directly. AddToCartButton ships to the browser because it needs onClick and useState, neither of which means anything without a live browser session.

What each side can and can’t do

A Server Component’s superpower is direct backend access with async/await built in:

export default async function Comments({ postId }) {
  const comments = await db.comments.findByPost(postId);
  return (
    <ul>
      {comments.map((c) => (
        <li key={c.id}>{c.text}</li>
      ))}
    </ul>
  );
}

No useEffect, no loading state managed by hand, no client-side fetch round trip — the component simply awaits its data before React ever renders it. But that same component has firm restrictions: no useState, useEffect, useRef, or any hook built on them, and no onClick, onChange, or other DOM event handlers, because there is no persistent, running instance of this component in a browser for a hook to attach to or an event to fire on. Try to put onClick on a button inside a Server Component and the framework will error, telling you to move that piece into a Client Component.

A Client Component, in exchange for losing direct backend access, gets everything that requires a live browser: state, effects, refs, event handlers, and browser APIs like window or localStorage.

The boundary is one-directional in one more way worth naming explicitly: a Server Component can import and render a Client Component directly (as ProductPage does above), but a Client Component cannot import a Server Component’s module and render it the same way, because by the time that code is running in the browser, the Server Component’s module — and its database access — was never shipped there at all. A Client Component can still display a Server Component’s output, but only by receiving it as children or another prop passed down from a Server Component ancestor, never by importing it itself.

What the video covers

Theo’s video is a direct explanation of the RSC model itself — the “two computers” framing used above, the 'use client' boundary, and the specific capability split (no hooks or browser APIs on the server, direct backend access that a Client Component doesn’t get) — which is exactly this lesson’s scope rather than a Next.js how-to. It runs long, around 53 minutes, and is best treated as a deep dive once the summary above makes sense, not a first pass. Its examples are built inside Next.js’s App Router, since that’s currently the most common place to encounter RSCs in practice, but the server/client execution split and the rules around it belong to React itself, not to Next.js’s routing or file conventions.

Where this fits

With the server/client split established, the next lesson covers what happens when a Server Component needs to accept a mutation from the user — Server Actions and Mutations.

Check yourself

  1. 01If a `.jsx` file has no directive at the top at all, is it a Server Component or a Client Component?

    It is a Server Component. The default, with no directive present, is to run only on the server; a file must explicitly add `'use client'` to become a Client Component.

  2. 02Why can't a Server Component use `useState`?

    `useState` exists to trigger a re-render in a running browser session and persist a value across renders. A Server Component runs once on the server to produce output and is gone; there is no running client session for a hook like `useState` to attach to.

  3. 03Why is directly querying a database from inside a component considered safe in a Server Component but unsafe in a Client Component?

    A Server Component's code, including any database client and credentials it imports, runs only on the server and is never sent to the browser. The same code inside a Client Component would ship in the JavaScript bundle, exposing the query logic and any embedded secrets to anyone who opens the browser's dev tools.

  4. 04Can a Client Component import and render a Server Component's module directly?

    No. The boundary only goes one way: a Server Component can render a Client Component, but a Client Component cannot import a Server Component module itself. It can still display Server Component output if that output is passed to it as `children` from further up the tree.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark