React Server Components – A Better Way to Render on the Server

What are React Server Components?
React Server Components (RSC) are components that are rendered on the server, with no JavaScript sent to the client for those components. This reduces the size of the client-side JavaScript bundle, leading to faster load times. Server Components can also interact with your backend without needing to be rehydrated on the client side.
How do React Server Components work?
- Server Components are rendered on the server.
- They can be sent directly to the client as HTML without JavaScript.
- They can fetch data, perform computations, or interact with APIs on the server without having to send this logic to the client.
Example: Using React Server Components
Here’s an example of a React Server Component setup:
- Create a Server Component: We create a component that will run only on the server and fetch some data. For simplicity, let’s say it’s fetching some posts from an API.
// components/ServerComponent.js import React from 'react'; async function fetchPosts() { const response = await fetch('https://jsonplaceholder.typicode.com/posts'); return response.json(); } export default function ServerComponent() { const posts = await fetchPosts(); // This runs on the server return (
Posts
- {posts.map((post) => (
- {post.title}
))}
); }
In this example:
- ServerComponent fetches data from an API (jsonplaceholder) on the server.
- The data is fetched on the server side, and only the resulting HTML is sent to the client, making the page load faster.
- Create a Client Component: Client-side components can also be used alongside Server Components. For instance, let’s create a simple button that works on the client side.
// components/ClientComponent.js
import React, { useState } from 'react';
export default function ClientComponent() {
const [count, setCount] = useState(0);
return (
Count: {count}
); }
3.Rendering Server and Client Components Together: React Server Components can be combined with regular React components. Here, we render the ServerComponent on the server and the ClientComponent on the client.
// pages/index.js
import React from 'react';
import dynamic from 'next/dynamic';
import ServerComponent from '../components/ServerComponent';
const ClientComponent = dynamic(() => import('../components/ClientComponent'), { ssr: false });
export default function HomePage() {
return (
); }
In this setup:
- ServerComponent runs on the server and fetches posts.
- ClientComponent is loaded on the client, and it handles interactive client-side functionality (like incrementing a count).
Advantages of Server Components:
- Reduced JavaScript Bundle Size: Only HTML is sent to the client for server-rendered components, reducing the initial load time.
- Better Performance: Offloading rendering and data fetching to the server means faster rendering for the client.
- Simplified Data Fetching: You can fetch data directly within Server Components, making it easier to build full-stack applications without complex client-side logic.
Conclusion:
React Server Components are an innovative feature that allows developers to better manage server-side and client-side rendering. They help in reducing the JavaScript bundle size and enhancing the performance of your React applications. By combining Server and Client components, you can create applications that are faster, more responsive, and easier to maintain.
This feature is still experimental, but it’s a glimpse into the future of React, where developers can choose to offload rendering to the server while keeping a minimal client-side footprint.