الانتقال إلى المحتوى
FrontendReact· 6 دقائق للقراءة

'use client' Doesn't Mean Client-Only: Server and Client Components Explained

The directive decides which code ships to the browser, not where it renders: Client Components still run on the server to build the first HTML.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
  • React 19
محتويات المقال

A team moves its dashboard to a framework built on . The theme toggle already starts with 'use client', so nobody worries when its useState initializer reads window.localStorage. Then the server logs fill with ReferenceError: window is not defined on every fresh page load. In the same week, someone opens the page source and is puzzled to find the markup of the "client" charts sitting right there in the HTML. Both surprises come from the same assumption about what the directive means.

The common misconception

The belief: 'use client' makes a component render only in the browser. It never runs on the server, so it never appears in the server-rendered HTML, and its code can use window freely.

The name practically says so, and React's own reference defines Client Components as components "rendered on the client". Server Components really can't use state or event handlers, so "client" reads like "the browser and nothing else". And before Server Components, "client-side rendering" meant exactly that: an empty page that JavaScript fills in. The half that's true: a Client Component's code is sent to the browser, and only there can it become interactive. What's missing is that the browser may not be the only place it renders.

What actually happens

Two servers, two different jobs

An app with Server Components can involve two server-side steps, and they do different things.

The first is the Server Components environment. React's reference describes Server Components as rendering ahead of time, before bundling, "in an environment separate from your client app or SSR server", either once at build time or for each request. While rendering there, React runs only Server Components. When it reaches a component imported from a client module, it doesn't run it; it leaves that part of the tree, with its props, for the client to render. Server Components never run in the browser, and their code is never shipped there.

The second step is optional: . React's docs say the combined result "can then be server-side rendered (SSR) to create the initial HTML for the page". That pass renders the whole tree to HTML, Client Components included, just as react-dom/server always has. In the browser, React then that HTML: it renders the Client Components again, adopts the existing DOM and attaches event handlers. So on a server-rendered page, one Client Component can render twice per load: once on the server for HTML, once in the browser to hydrate.

'use client' marks a module boundary, not a runtime

The directive goes at the very top of a file and marks that module and its transitive dependencies as client code. When a Server Component imports such a file, compatible bundlers treat the import as the boundary between server-run and client-run code. It answers "which code must be bundled and sent to the browser?", not "where does this render?".

The boundary lives in the module graph, not the render tree. A Server Component passed as children to a Client Component stays a Server Component: it has already rendered by the time the Client Component runs. And everything crossing the boundary as props must be serializable, so plain data, JSX and promises work, but ordinary functions and class instances don't.

Browser-only code needs a plan

Since a Client Component may render on the server, its render logic, including useState initializers, must work without window, document or localStorage. Effects are safe: React's docs state that Effects only run on the client. For a component that can't produce meaningful HTML at all, React 19.3 added browser() from react-dom. Calling use(browser()) inside a <Suspense> boundary makes server rendering stop at that component and leave the boundary's fallback in the HTML; in the browser it returns undefined and the component renders normally.

TSX ThemeToggle.tsx
"use client";import { useState } from "react";// Assumes 'use client' keeps this out of the server renderexport function ThemeToggle() {  const [theme, setTheme] = useState(    () => window.localStorage.getItem("theme") ?? "light",  ); // SSR: ReferenceError: window is not defined  const next = theme === "dark" ? "light" : "dark";  return <button onClick={() => setTheme(next)}>Theme: {theme}</button>;}

The right-hand version puts a disabled placeholder in the HTML and renders the real toggle once the browser takes over. use(browser()) must be called from a Client Component, and without a Suspense boundary around it the server render fails. On React versions before 19.3, the documented alternative is two-pass rendering: render the server's output first, then switch to browser values in an Effect.

QuestionServer ComponentClient Component ('use client')
Runs in the Server Components environment?Yes, at build time or per requestNo: it's left, with its props, for the client
In the server-rendered HTML?Its output, when the app uses SSRYes, when the app uses SSR
Runs in the browser?NeverYes: it hydrates, then re-renders on updates
Code shipped to the browser?NoYes, with everything it imports
State, Effects, event handlers?NoYes

How to apply it in your project

  1. Audit render code in client modulesSearch files with 'use client' for window, document, localStorage, navigator and matchMedia in component bodies and useState initializers. Each hit can throw on the server or cause a mismatch.
  2. Move browser reads out of renderRead browser APIs in Effects or event handlers, and render the same default on both sides until then.
  3. Opt out explicitly where HTML is meaninglessOn React 19.3 or later, wrap components that only make sense in the browser in <Suspense> and call use(browser()), instead of typeof window checks or a mounted flag.
  4. Push the directive downMove 'use client' from page-level files to the interactive leaves, and check the props that cross the boundary are serializable.

Key takeaways

  • 'use client' marks a module boundary: that file and its imports are bundled and shipped to the browser.
  • On a server-rendered page, Client Components also render on the server to produce the first HTML, then hydrate.
  • Server Components run in their own environment, never in the browser, and their code never ships.
  • Browser-only code belongs in Effects, event handlers or behind use(browser()), not in render or typeof window checks.

📚 المصادر

  1. 'use client' — React Server Components Reference
  2. Server Components — React Reference
  3. browser — React DOM API Reference
  4. React 19.3 — React Blog
  5. hydrateRoot — React DOM API Reference

التعليقات (0)