WhoBuilt

React 'use client' visually explained: What, Why, How

Oct 1, 2024· 17 min· 1.6K views·Watch on YouTube

Speaker

Delba de Oliveira explains React’s “use client” directive and server-client rendering at React Universe Conf 2024, in a recorded talk for Callstack.

Why split work between server and client

Bundling groups imported modules into code suited to where it will run, and can split code by route or environment. Servers can access databases and secrets, parse Markdown, and prerender pages; browsers can use DOM and browser APIs and respond immediately to user input.

How use client marks a boundary

In a server-first framework such as Next.js, route modules and their imports begin in the server environment. Adding “use client” at the top of a file marks an entry point into the client environment, and its imports become client modules too. Marking every imported file separately has no additional bundling effect.

What crosses the boundary

When a server module imports from a client module, it receives a reference to the client export rather than the value itself; React renders the client component in the client environment. “Use server” marks functions that client code can call, with React handling the communication layer, and is not a way to mark server components.

Nesting server-rendered UI

A client module cannot import a server component, since that could require another server round trip after client rendering begins. Instead, a server component can be passed to a client component as a prop, so the server can render both in one pass. For example, a server-rendered data card can appear inside a client-side modal that toggles visibility.

Prerendering and trade-offs

Client components may also be prerendered on the server, letting users see and navigate the initial HTML while JavaScript loads and React hydrates it. Unchanging content can be prerendered at build time. De Oliveira says the model simplifies how developers compose UI across environments, while acknowledging rough edges and that it does not replace back-end tools for needs beyond serving UI.

Takeaways

  • Mark client entry points where browser capabilities or immediate interaction are needed; imports into that entry point join the client bundle.
  • Keep data access, secrets, and suitable prerendering on the server, and send the rendered result or component reference across the boundary.
  • Use server actions for client-triggered server functions; they handle mutations and are distinct from server components.
  • Pass server-rendered components as props to client components to avoid additional server round trips.
  • Client components can be prerendered for faster initial display, then hydrated in the browser.

Mentioned in this talk

Search inside this talk

What was claimed 6

  • feature
    Callstack: Sponsors the conference
    “This conference is brought to you by Callstack”
    YouTube
    Unverified1 source· Talk
  • feature
    React: Extends its component model to the server, allowing server and client UI concerns to use the same paradigm
    “React can now manage both sides of the network within the same paradigm.”
    YouTube
    Unverified1 source· Talk
  • feature
    React use client directive: Marks the boundary between server-run and client-run code
    “the `use client` directive serves this purpose”
    YouTube
    Unverified1 source· Talk
  • feature
    React Server Actions: Mark server-side functions that can be called from client-side code
    “mark server-side functions that can be called from client-side code”
    YouTube
    Unverified1 source· Talk
  • feature
    React Server Components: can be passed to client components as props and rendered in a single server pass
    can be passed to client components as props and rendered in a single server pass
    “React supports passing server components to client components as props.”
    YouTube
    Unverified1 source· Talk
  • feature
    React: Client components can be pre-rendered on the server to improve the initial page loading experience
    “they may also be pre-rendered on the server to improve the loading experience”
    YouTube
    Unverified1 source· Talk

Related talks