What you need to know about React Server Component?


Developers frequently have to choose between performance and SEO while creating conventional client-side-only React apps. Server components give developers the ability to utilize the server infrastructure more effectively and, thus, by default, attain excellent performance.

By combining the most beneficial aspects of client-side interaction and server-side rendering, React Server Components enable programmers to create apps that are easy to create and maintain, and they can be shared across different projects.

In this article, we'll take a look at what React Server Components are and how they can be used to create isomorphic applications.

What Are React Server Components?

React Server Components (RSCs) are a new type of component that allows you to create reusable components that can be rendered on the server. RSCs are designed to make it easier to create isomorphic (universal) applications, which are applications that can be rendered on both the server and the client.

Project dependencies, for instance, might now remain on the server only rather than affecting the size of the JavaScript package on the user.

React server components have a few advantages over traditional server-side rendering. To be specific -

  • These server components are often used to create reusable components that can be shared across different projects or applications.

  • With RSCs, you can write code that is shared between the server and the client, which makes isomorphic applications simpler to develop.

  • RSCs also allow you to create stateful components that can be hydrated on the client, which means that you can create isomorphic applications that are faster and more responsive.

Server Components vs Client Components in React

In React, a component is a piece of UI that can be reusable. There are two types of components: server components and client components. Server components are rendered on the server, while client components are rendered on the client.

Each type of component has its advantages and disadvantages. Server components are generally faster and more reliable, but they can be more difficult to develop and deploy. Client components are easier to develop and deploy, but they can be slower and less reliable.

Client-side components don’t implement the following features which React Server Components have -

  • Utilization of server-only data sets, including file systems, databases, and internal services. In other words, the element has total exposure to information about the nodes where it is located.

  • Integrating server hooks can be used to reach server-side resources such as the system files and distribute functionality in a similar way to regular hooks.

  • RSCs are part of the hierarchical element tree that includes both client and server components stacked inside one another.

When we talk about some constraints of RSCs, let’s see what are they-

  • No state application is allowed (useState(), for instance, isn't supported). What for? Element isn't executing on the browser and preserving state since it runs once, and the outcomes are broadcast to the browser.

  • The useEffect lifecycle action is absent (). Once more, this is why the element also isn't operating in the window, where it would benefit from events and side effects.

  • No DOM or browser-specific APIs, provided you polyfill those on the client. Consider the retrieve API in particular, whereby server-side rendering algorithms frequently offer a polyfill to make the server-side function appear identical to the client in terms of API requests.

Ultimately, the decision of whether to use server components or client components depends on your specific needs and preferences. If you need a fast-loading website, then server components are the way to go. If you need a website that uses fewer resources, then client components are the way to go.

React Server Components: Use Cases

According to the React team, Server components and SSR work well together. SSR is a method for swiftly displaying client elements in any non-interactive state. After the first HTML is retrieved, you are still responsible for the expense of downloading, processing, and running those Client Parts.

RSCs attempt to completely substitute the client-side functions with activities undertaken on the servers, compared to SSR, where you are attempting to send out a preliminary form of an element that subsequently operates like a regular client-side component.

It has two primary advantages -

  • The packaged JavaScript doesn't require to be sent over the network to the browser. Here on the client, the JavaScript is loaded, run, and the output is received.

  • Making initial API/ Ajax queries to activate the element and then, the element can communicate immediately with back-end applications to meet these requirements. Doing this makes the client less talkative and prevents the "waterfall of queries" from occasionally occurring as the website completes linked data requests.

Workings of React Server Components

The specifics of how this functionality will be implemented may change since React Server Components has been in the experimental stage. Nevertheless, we can pick up a few of its fundamental ideas.

As you navigate to the coding within the src/ folder, users will see three different types of component filenames -

  • A Server Components is identified by the .server.js extension.

  • React Client Components are identified by the .client.js extension.

The standard.js extension designates standard components. Those components could execute on the client or the browser based on who is exporting them. Two jobs simultaneously run when you use the npm begin command to launch the software -

  • The api.server.js/ script server used by the Node server.

  • The Web page is compiled for your client-side React components employing the build.js/ scripts code.

Conclusion

This brand-new intriguing feature has the potential to alter how developers create React applications. It facilitates the importation of packages into React apps without increasing the client's package size, resulting in a stable version of the app.

However, it should be noted that the React Server Components in no manner displace other React community components, notably, standard client components.

Updated on: 07-Dec-2022

759 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements