Skip to content

React Server Components Patterns and Data Fetching Architecture

React Server Components (RSC) is a major shift in React architecture. It allows components to render on the server, directly accessing backend resources, reducing client-side JavaScript bundle size and data fetching complexity.

RSC Core Concepts ​

RSC divides into two types of components:

typescript
// Server component - default
async function UserProfile({ userId }) {
  const user = await db.user.findById(userId);  // Direct database access
  return (
    <div>
      <h2>{user.name}</h2>
      <p>{user.email}</p>
    </div>
  );
}

// Client component - needs 'use client' directive
'use client';
function LikeButton({ postId }) {
  const [liked, setLiked] = useState(false);
  
  return (
    <button onClick={() => setLiked(!liked)}>
      {liked ? '❤️' : '🤍'}
    </button>
  );
}

Server component code is never sent to the client—clients only see rendered HTML.

Data Fetching Patterns ​

Pattern 1: Direct database access

typescript
async function ProductList() {
  const products = await db.product.findMany({
    orderBy: { createdAt: 'desc' },
    take: 20
  });

  return (
    <ul>
      {products.map(product => (
        <li key={product.id}>
          <ProductCard product={product} />
        </li>
      ))}
    </ul>
  );
}

Pattern 2: API route calls

typescript
async function UserProfile({ userId }) {
  const response = await fetch(`https://api.example.com/users/${userId}`);
  const user = await response.json();
  return <UserDetails user={user} />;
}

Pattern 3: Caching strategies

typescript
async function CachedData() {
  const data = await fetch('https://api.example.com/data', {
    next: { revalidate: 3600 }  // Revalidate every hour
  });
  return <DataDisplay data={data.json()} />;
}

Streaming Rendering ​

typescript
import { Suspense } from 'react';

async function Dashboard() {
  return (
    <div>
      <header>Site Title</header>
      <QuickStats />
      <Suspense fallback={<Loading />}>
        <SlowAnalytics />
      </Suspense>
      <Suspense fallback={<Loading />}>
        <RecentOrders />
      </Suspense>
    </div>
  );
}

Users see the page shell and the fast-loading content first, while slower data streams in progressively.

Error Handling ​

typescript
// Server component error boundary
async function UserData({ userId }) {
  try {
    const user = await db.user.findById(userId);
    if (!user) return <NotFound />;
    return <UserProfile user={user} />;
  } catch (error) {
    return <ErrorFallback error={error} />;
  }
}

Performance Optimization ​

Strategy 1: Reduce client-side JavaScript

typescript
// Good practice: logic on server
async function DataDisplay() {
  const data = await fetchData();
  const processed = processData(data);  // Server-side processing
  return <Chart data={processed} />;
}

Strategy 2: Parallel data fetching

typescript
async function Dashboard() {
  const [stats, activity, notifications] = await Promise.all([
    getStats(),
    getRecentActivity(),
    getNotifications()
  ]);

  return (
    <div>
      <Stats stats={stats} />
      <Activity activities={activity} />
      <Notifications items={notifications} />
    </div>
  );
}

Summary ​

React Server Components make data fetching simpler and more efficient. Server components directly access backend resources, client components handle interaction logic. 2026's RSC principles: correct component boundaries, reasonable caching strategies, parallel data fetching.

MIT Licensed