keepcalculator

06 Numbers and Powers

React Server Components: What Changes, What Stays on Client

Card No. 06·72830 · Updated · Standard formula, shown below

Moving to React Server Components, the main change is that components run on the server by default, fetching data and rendering to a serializable format before sending HTML to the client; what stays on the client are interactive components that use state, effects, and event handlers. This shifts a significant portion of your app’s logic and data fetching to the server, reducing the JavaScript bundle sent to the browser.

Background: Why Server Components?

React Server Components (RSC) were introduced to address the growing problem of large JavaScript bundles. In a traditional React app, every component—even those that only render static content—is shipped to the client as JavaScript. This increases load time, especially on slower devices. RSC allows components to run on the server, sending only the rendered output (a serializable tree) to the client. This reduces the amount of JavaScript the browser needs to download, parse, and execute.

The concept was first demonstrated in a 2020 research talk by the React team, and later integrated into frameworks like Next.js (from version 13) and Remix. It’s not a replacement for the whole React model but an addition that lets you choose where each component runs.

How It Works: The Two Types of Components

In RSC, you have two explicit component types:

  • Server Components: Run only on the server. They can directly access databases, file systems, or internal services. They cannot use state, effects, or event handlers. They render to a special format that the client can interpret, but they do not add to the JavaScript bundle.
  • Client Components: Run on the client (and optionally on the server for initial render). They are the only ones that can use useState, useEffect, and handle user interactions. They are marked with a directive like 'use client' at the top of the file.

When a server component imports a client component, the client component is still sent to the browser, but the server component’s own code is not. This means you can have a server component that fetches data and passes it as props to a client component that displays it interactively.

What Changes: Data Fetching and Rendering

The most significant change is where data fetching happens. In a traditional React app, data fetching often occurs in useEffect on the client, leading to loading states and extra round trips. With RSC, you can fetch data directly inside a server component using async/await or any server-side data access method. The server fetches the data, renders the component, and sends the final HTML to the client. This eliminates client-side loading spinners and reduces the amount of code shipped.

Another change is the mental model: you no longer think of “the app” as one big client-side unit. Instead, you decide which parts are static (server) and which are interactive (client). This often leads to a smaller overall bundle and better performance, but it requires a shift in how you structure components.

What Stays on the Client: Interactivity

Anything that requires interactivity must be a client component. This includes:

  • State management (e.g., useState, useReducer)
  • Effects (useEffect, useLayoutEffect)
  • Event handlers (onClick, onChange)
  • Browser-only APIs (like window, document)
  • Context providers that need to change over time

In practice, this means your input fields, buttons, sliders, and any component that updates based on user input will be client components. Even a simple counter needs to be a client component because it uses state. However, you can often keep the surrounding layout and static content as server components, passing the interactive pieces as children.

How to Choose: Server vs. Client

A good rule of thumb is to start with server components by default and only opt into client components when you need interactivity. Ask yourself:

  • Does this component use state or effects? → Client
  • Does it handle user events? → Client
  • Does it rely on browser-only APIs? → Client
  • Does it fetch data that doesn’t need to be real-time? → Server
  • Does it render static content? → Server

A common pattern is to have a server component that fetches data and maps over items, rendering a client component for each item that has interactive features. For example, a blog list could be a server component that fetches posts, and each post could be a client component with a “like” button.

Common Problems and Misconceptions

One common misconception is that RSC means you can’t use state at all. That’s false—you can, but only in client components. Another is that all components must be either server or client; in reality, you can mix them freely.

Another issue is the “server-only” import. Some libraries that rely on Node.js APIs cannot be used in client components. Conversely, client components cannot import server components directly (they can only be passed as children). This can lead to confusion when trying to share code between the two.

Also, be careful with serialization: props passed from server to client components must be serializable (no functions, no Date objects without proper handling). This means you can’t pass a database connection or a class instance from a server component to a client component.

Comparing to Other Approaches

RSC is often compared to other data-fetching and rendering strategies:

  • Traditional client-side rendering (CSR): All rendering happens on the client. RSC reduces the bundle size and moves data fetching to the server, which can improve load times.
  • Server-side rendering (SSR): In SSR, the server renders the entire app to HTML, but the client still downloads the full React bundle to hydrate. RSC goes further by not sending the component code for server components at all.
  • Static site generation (SSG): SSG pre-renders at build time. RSC can be used with SSG to pre-render server components while still allowing dynamic client components.

RSC is not a replacement for SSR or SSG; it’s a complementary feature that can be used alongside them.

Costs and Trade-offs

The main cost is complexity. You now have two types of components, each with its own rules. This can make code harder to reason about, especially for developers new to the concept. There’s also a learning curve for understanding what can be passed between server and client.

Another cost is server load: since more work happens on the server, you need to ensure your server can handle the load. Caching strategies become important to avoid redundant data fetching. However, for many apps, the performance benefits outweigh these costs.

Safety and Best Practices

When using RSC, be mindful of what you expose to the client. Server components can access sensitive data (like database credentials) that should never be sent to the browser. Ensure that any data passed to client components is sanitized and necessary. Also, avoid using server-only libraries in client components, as this can cause errors.

It’s also important to test both server and client components thoroughly. Since server components run in a different environment, you may need to mock certain APIs in tests. Use the built-in testing tools from your framework to handle both.

Frequently Asked Questions

Can I use React Server Components with my existing React app?

Yes, but you’ll need to use a framework that supports RSC, such as Next.js or Remix. The React core team has also released a canary version of React that includes RSC support, but it’s not yet stable for all use cases. You’ll likely need to restructure your components to separate server and client concerns.

Do server components work with TypeScript?

Yes, TypeScript works with RSC. You can type your props and data fetching as usual. However, you need to be careful with the serialization boundary—only serializable types can be passed from server to client. The React team provides type definitions that help enforce this.

How do I handle forms with server components?

Forms that require interactivity (like live validation) should be client components. However, you can use server actions (a related feature) to handle form submissions on the server without writing client-side event handlers. Server actions allow you to define async functions that run on the server and can be called from client components.

What is the difference between server components and server-side rendering?

Server-side rendering (SSR) renders the entire React tree to HTML on the server, but the client still downloads the full JavaScript bundle to hydrate. Server components, on the other hand, do not send their code to the client at all. The client only receives the rendered output, which reduces the bundle size significantly.

Can I use third-party libraries that rely on state in server components?

No. If a library uses state, effects, or browser APIs, it must be used in a client component. You can still use it in your app by wrapping it in a client component and then importing that client component into a server component. Many popular libraries have started adding RSC-compatible versions or guidance.

Is there a performance penalty for using client components?

Client components add to the JavaScript bundle, so there is a performance cost. However, you can minimize this by keeping client components small and using server components for the bulk of your UI. The goal is to ship only the code that needs to run on the client.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →