Back to articles

Mastering React Server Components: A Practical Guide

AuthorMajd Muhtaseb12/03/20258 minutes

Introduction

React Server Components (RSCs) represent a paradigm shift in how we build React applications. Instead of running all our component logic in the browser, RSCs execute on the server, reducing the amount of JavaScript sent to the client and improving initial page load times. This guide will provide a practical overview of RSCs and how to use them effectively.

What are React Server Components?

RSCs are React components that render on the server. They have several key benefits:

  • Reduced Client-Side JavaScript: RSCs don't require client-side JavaScript, leading to smaller bundle sizes.
  • Direct Data Access: RSCs can directly access data sources like databases without the need for API endpoints.
  • Improved Performance: Faster initial load times and improved perceived performance for users.

Key Differences from Client Components

| Feature | Server Components | Client Components | | ----------------- | ------------------------------------------------------- | ---------------------------------------------------- | | Execution Environment | Server | Browser | | Interactivity | Not interactive (no event handlers) | Interactive (can use event handlers, useState, etc.) | | Bundle Size | Doesn't contribute to the client-side bundle | Adds to the client-side bundle | | Data Fetching | Can directly fetch data from databases or file systems | Requires API calls to fetch data |

A Simple Example

Here's a basic example of a React Server Component:

// app/components/ServerComponent.js (Server Component)
import { sql } from '@vercel/postgres';

export default async function ServerComponent() {
  const products = await sql`SELECT * FROM products`;

  return (
    <div>
      <h1>Products</h1>
      <ul>
        {products.rows.map((product) => (
          <li key={product.id}>{product.name}</li>
        ))}
      </ul>
    </div>
  );
}

// app/components/ClientComponent.js (Client Component)
'use client';

import { useState } from 'react';

export default function ClientComponent({ initialCount }) {
  const [count, setCount] = useState(initialCount);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

In this example, ServerComponent fetches data directly from a database. It's important to note that you can't directly import client components into a server component. However, you can pass data fetched in a server component down as props to a client component.

Using Server and Client Components Together

The real power comes from combining Server Components and Client Components. Data fetched server-side can be efficiently passed to client components for interactive elements.

// app/page.js
import ServerComponent from './components/ServerComponent';
import ClientComponent from './components/ClientComponent';

export default async function Page() {
  return (
    <div>
      <ServerComponent />
      <ClientComponent initialCount={0} />
    </div>
  );
}

Key Takeaways

  • React Server Components execute on the server, reducing client-side JavaScript and improving performance.
  • Server Components can directly access data sources.
  • Combine Server Components and Client Components for optimal results.

By mastering React Server Components, you can build faster, more efficient React applications that provide a superior user experience.