What Are React Server Components ?
Rohit Kumar Meena
SDE II @ Allen digital | 5+ years in frontend Engineering (React & Next JS, Typescript) | Worked at Times Internet, insurancedekho, collegedunia | NIT-A
React Server Components (RSC) are a cutting-edge feature introduced to enhance the efficiency and performance of React applications. They allow developers to render components on the server while seamlessly integrating with client-side interactivity. This article dives deep into what React Server Components are, their advantages, and practical examples of how to use them.
React Server Components are components that are rendered on the server and sent to the client as HTML and serialized data. Unlike traditional Server-Side Rendering (SSR), where the entire page is rendered on the server, RSC enables a hybrid model where only specific components are rendered server-side, reducing the need for client-side JavaScript.
Key Features of React Server Components
When to Use React Server Components
How React Server Components Work
React Server Components require a server environment to render the components and send the resulting HTML and data to the client. They can be used in frameworks like Next.js (from version 13 onward) that support RSC out of the box.
File Structure in Next.js
With Next.js’s app directory, you can organize your server and client components as follows:
app/
page.js // Root server component
components/
Header.jsx // Client component
Content.jsx // Server component
Examples of React Server Components
1. Server Component for Data Fetching
// app/components/Content.jsx
export default async function Content() {
const data = await fetch(
'https://jsonplaceholder.typicode.com/posts/1'
).then((res) => res.json());
return (
<div>
<h2>{data.title}</h2>
<p>{data.body}</p>
</div>
);
}
This server component fetches data from an API and returns HTML. The fetching happens on the server, and only the result is sent to the client.
领英推荐
2. Combining Server and Client Components
// app/components/Header.jsx
"use client"; // Mark this as a client component
export default function Header() {
return (
<header>
<h1>React Server Components Demo</h1>
</header>
);
}
// app/page.js
import Header from './components/Header';
import Content from './components/Content';
export default function Page() {
return (
<div>
<Header />
<Content />
</div>
);
}
Here, the Header component is a client component, while the Content component is rendered on the server. This hybrid approach allows developers to optimize performance without sacrificing interactivity.
3. Using Props with Server Components
Server components can accept props, but those props must be serializable.
// app/components/Greeting.jsx
export default function Greeting({ name }) {
return <h2>Hello, {name}!</h2>;
}
// app/page.js
import Greeting from './components/Greeting';
export default function Page() {
return (
<div>
<Greeting name="John" />
</div>
);
}
Benefits of React Server Components
Best Practices for Using RSC
Limitations of React Server Components