
'use client' Doesn't Mean Client-Only: Server and Client Components Explained
- كاتب موثّق
- React 19
محتويات المقال
'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
'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.
What actually happens
Two servers, two different jobs
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
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
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.
"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>;}"use client";import { Suspense, use, useState } from "react";import { browser } from "react-dom";function SavedTheme() { use(browser("The theme lives in localStorage")); // server: fallback const [theme, setTheme] = useState( () => window.localStorage.getItem("theme") ?? "light", ); const next = theme === "dark" ? "light" : "dark"; return <button onClick={() => setTheme(next)}>Theme: {theme}</button>;}export function ThemeToggle() { return ( <Suspense fallback={<button disabled>Theme</button>}> <SavedTheme /> </Suspense> );}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.
'use client') | ||
|---|---|---|
How to apply it in your project
Audit render code in client modules Search files with 'use client'forwindow,document,localStorage,navigatorandmatchMediain component bodies anduseStateinitializers. Each hit can throw on the server or cause a mismatch.Move browser reads out of render Read browser APIs in Effects or event handlers, and render the same default on both sides until then. Opt out explicitly where HTML is meaningless On React 19.3 or later, wrap components that only make sense in the browser in <Suspense>and calluse(browser()), instead oftypeof windowchecks or a mounted flag.Push the directive down Move '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 ortypeof windowchecks.
التعليقات (0)