Understanding React Server Component
Introduction
As most of us already know, React enables us to break down interfaces into independent parts called Components, facilitating reuse and customization. Currently, React Components are divided into two main types:
- Client Components: widely used in Client Side Rendering (CSR) applications.
- Server Components: a new type of component that renders on the server side before being sent to the client (browser). At first glance, it may seem similar to Server Side Rendering (SSR) which has been used for a while, but in reality, these two concepts have certain differences.
The Difference of RSC (React Server Component)
- Ease of development: React Server Component allows easy direct connection to server-side resources such as databases or internal services, streamlining development processes.
- Improved performance: By reducing latency in communication between client and server, React Server Component enhances overall application performance.
- Reduced source code size: Libraries utilized only on the server side need not be transmitted to the client, resulting in a lighter source code footprint.
- Additionally, Server Components automatically split the source code into smaller parts, allowing clients to load only necessary sections, thus optimizing performance. This means we no longer need to use techniques like lazy loading as previously required.
Just import the components directly that we need to use:
Comparing Server Side Rendering with React Server Component
- In both Server Side Rendering (SSR) and Server Component, rendering occurs on the server side. However, Server Side Rendering only returns raw HTML upon initial page load. On the other hand, Server Component is treated as an actual component, meaning we can define multiple Server Components, use them in various places, and reuse them multiple times.
- With Server Component, the result received from the server is merged with the current interface by React to create a new interface.
Limitations of Server Component
- Since Server Component is created on the server side, it cannot be used with web browser APIs like accessing window or document variables.
- Also, React APIs such as useState, useContext, useEffect, etc., cannot be used, resulting in Server Component not supporting user-interactable events in the application.
Example Usage
First, create a file app/demo/Todo.tsx.
Next, create a file app/demo/AddTodo.tsx.
Finally, create a file app/demo/page.tsx, where the getTodoData function is used to directly fetch the todo list from the server. We can query the database to fetch the data right here.
When you successfully start the application, in the Sources tab, you'll only see the AddTodo.tsx file (Client Component) in the client-side source code, without the Todo.tsx file (Server Component).
Bundle response of client side |
In Summary
- React Server Component is a new feature supported by Next from version 13, currently still in development and testing phase. You should consider carefully if you want to use it in a production environment.
- However, once the feature becomes stable, it promises to be a significant development step for React, providing better support for SSR and optimizing React applications further.
Comments
Post a Comment