Skip to article content
FrontendXLesson 22 / 40

Server Components and the RSC Model

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

advanced · about 60 minutes · 4 exercises

  • Predict
  • Implement
  • Debug
  • Interview

What you will be able to do

#
  • Distinguish server components from client components.
  • Draw the client boundary with the use client directive.
  • Explain what can cross the boundary as props.

Overview

#

Two kinds of components

A server component runs on the server and sends a description of UI to the client. It never ships its code to the browser, so it can read a database or a file directly and keep secrets out of the bundle. A client component runs in the browser and can use state, effects and event handlers.

Async server components

A server component can be an async function and await data directly. There is no use hook and no effect; the data is ready before the UI is sent.

snippet.tsx
async function Page({ id }: { id: string }) {
  const post = await db.post.findUnique({ where: { id } });
  return <article>{post.title}</article>;
}

The client boundary

A file marked with the use client directive, and everything it imports, becomes a client component. This is the boundary between the two worlds. Client components can be rendered by server components, but a server component cannot be imported into a client component.

snippet.tsx
'use client';

import { useState } from 'react';

export function LikeButton({ initial }: { initial: number }) {
  const [likes, setLikes] = useState(initial);
  return <button onClick={() => setLikes(likes + 1)}>{likes}</button>;
}

Props cross the boundary as data

Props passed from a server component to a client component must be serializable. You cannot pass a function unless it is a server action. Pass plain data, or pass a server action as a prop when the client needs to trigger server work.

Pitfall: marking everything client

Adding use client at the top of the tree turns the whole app into client components and gives up the benefits. Push the directive down to the leaves that need interactivity and keep the rest on the server.

When to use each

Server components for data access, large dependencies and static content. Client components for state, effects, event handlers and browser APIs.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    What happens when a client component tries to import this server component?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    async function Chart() {
      const data = await db.metrics();
      return <svg>{data.map((d) => <rect key={d.id} />)}</svg>;
    }
    Choose the best answer
  2. Exercise 2Implement

    Split this page so only the interactive button is a client component.

    starter.tsx
    async function Page() {
      const post = await getPost();
      return <article><h1>{post.title}</h1><LikeButton /></article>;
    }

    Reveal the reference answer
    reference.tsx
    async function Page() {
      const post = await getPost();
      return <article><h1>{post.title}</h1><LikeButton initial={post.likes} /></article>;
    }
    
    // LikeButton.tsx
    'use client';
    export function LikeButton({ initial }: { initial: number }) {
      const [likes, setLikes] = useState(initial);
      return <button onClick={() => setLikes(likes + 1)}>{likes}</button>;
    }

    Authored expected output: Page stays a server component; only LikeButton ships to the browser.

    The data fetch stays on the server, and the directive is pushed down to the one leaf that needs state. This keeps the bundle small and the data access server-side.

  3. Exercise 3Debug

    This server component fails to build. Explain and fix.

    starter.tsx
    async function Page() {
      const [open, setOpen] = useState(false);
      return <button onClick={() => setOpen(true)}>{open ? 'open' : 'closed'}</button>;
    }

    Reveal the reference answer
    reference.tsx
    'use client';
    
    export function Toggle() {
      const [open, setOpen] = useState(false);
      return <button onClick={() => setOpen(true)}>{open ? 'open' : 'closed'}</button>;
    }

    Authored expected output: The component builds and the button toggles state in the browser.

    Server components cannot use state or event handlers. Moving the interactive logic into a client component marked with use client is the fix.

  4. Exercise 4Interview

    Explain when you would choose a server component over a client component, and the cost of the wrong choice.

    starter.tsx
    // data access, interactivity, bundle size

    Reveal the reference answer
    reference.tsx
    Choose a server component for data access, large dependencies and content that does not need interactivity, because its code never ships to the browser and it can read the database directly. Choose a client component when you need state, effects, event handlers or browser APIs. The cost of the wrong choice is real: marking a data-heavy component as client ships its code and its dependencies, and marking an interactive component as server makes it impossible to attach handlers. The usual pattern is server components for the page and data, with small client leaves for interactivity.

    Authored expected output: A rule based on data access and interactivity, plus the bundle and capability costs of getting it wrong.

    The trade-off is bundle size and capability. Server components reduce shipped code; client components add capability. Match the component to the need.

Common misconceptions

#
  • Server components can use hooks and state. They cannot; use a client component for state and effects.
  • A server component can import a client component and vice versa. Server can render client, but a client component cannot import a server component.
  • use client makes a component run only on the client. It marks the boundary; the component still renders on the server for the initial HTML.
  • Any prop can cross the boundary. Props must be serializable; functions must be server actions.

Follow-up practice

#
  • react-hydration
  • react-actions-and-server-functions

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.