React is a popular library for building user interfaces on the web. It allows developers to create reusable components that can render dynamic data and handle user interactions. However, as web applications grow in complexity and size, developers face some challenges with React, such as:

To address these challenges, React introduced a feature called React Server Components (RSC) in December 2020. RSC is a type of React component that can run on the server and stream HTML to the browser. Unlike SSR, RSC does not require the sending of any JavaScript code to the browser, which reduces the bundle size and improves the performance of React applications. RSC also enables direct access to backend resources from the server, which simplifies data fetching and eliminates the need for client-side state management. Furthermore, RSC can seamlessly integrate with existing client-side React components, which preserves the rich interactivity and flexibility of React.

In this article, we will explore how RSC work, what benefits they offer, and how to use them in your React applications.

Client, Server, and Shared Components

RSC feature allows writing components as server and client components. React differentiates server and client using the file extensions.

Let’s see how:

The server component is rendered in a special format

When a server component is rendered on the server, it is not converted into an HTML string like SSR. Instead, it is serialized into a special JSON-like format that contains information about the component’s type, props, children, and references. This format is called Server Component Transport Format (SCTF)

The SCTF is designed to be compact and efficient for streaming over the network. It also supports features such as lazy loading, code splitting, hydration hints, error boundaries, and suspense boundaries.

The SCTF is sent to the browser as a stream of chunks that are parsed and rendered incrementally by a custom React renderer called React Flight DOM Client Renderer. This renderer can handle both server components and client components in the same tree. It also supports features such as event delegation, focus management, accessibility, and concurrent mode.

Zero Bundle Size Server Components

One of the main benefits of RSC is that they do not add any JavaScript code to your bundle size. This means that you can write as many server components as you want without worrying about the impact on your performance budget.

Server components are only executed on the server and never sent to the browser. The browser only receives the SCTF and the HTML that is generated by the server components. The browser does not need to know anything about the implementation details of the server components, such as their imports, dependencies, or logic.

This also means that you can use any Node.js library or API in your server components without having to install or configure any bundler or transpiler. You can also use modern JavaScript features such as ES modules, optional chaining, nullish coalescing, etc. without worrying about browser compatibility.

Backend Access

Another benefit of RSC is that it can access backend resources directly from the server. This simplifies data fetching and eliminates the need for client-side state management.

Server components can use any Node.js API or library to fetch data from various sources, such as databases, file systems, caches, etc. They can also use GraphQL queries or REST endpoints to communicate with other services or APIs. Server components can pass the fetched data as props to other components in the tree, either server components or shared components.

This means that you do not need to write any code to fetch data on the client side, such as using hooks, custom hooks, reducers, actions, sagas, thunks, etc. You also do not need to worry about caching, invalidating, updating, or synchronizing the data between the server and the client.

How to use React Server Components

To use RSC in your React applications, you need to follow some steps:

Conclusion

React Server Components are a feature that allows developers to build highly performant React applications with zero bundle size and direct backend access. RSC are rendered on the server and stream HTML to the browser without sending any JavaScript code. RSC can also access backend resources directly from the server without requiring any client-side state management. RSC can seamlessly integrate with existing client-side React components and preserve the rich interactivity and flexibility of React.

RSC are still in research and development and not ready for production use yet. However, they offer a promising direction for improving the performance and simplicity of React applications in the future.

If you want to learn more about RSC, you can check out these resources:

Sources

The title image was generated by the artificial intelligence tool Leonardo.ai using the prompt "A retro steampunk computer with source code text on its screen."