Skip to main content

React Server Components

React Server Components (RSC) let you write components that run exclusively on the server. They can directly access databases, file systems, and other server-only resources without exposing them to the client.
Server Components are part of the React Server architecture and work seamlessly with streaming server rendering APIs like renderToPipeableStream and renderToReadableStream.

What are Server Components?

Server Components are React components that:
  • Run only on the server - Never sent to the client as JavaScript
  • Can be async - Use async/await directly in the component
  • Access server resources - Databases, file systems, APIs without exposing credentials
  • Zero bundle size - Don’t add to client JavaScript bundle
  • Automatic code splitting - Client components are automatically split

Server vs Client Components

Component comparison table

Basic Usage

Creating a Server Component

Server Components don’t need any special directive - they’re the default:

Marking Client Components

Use 'use client' directive at the top of files that need client-side features:

Composing Server and Client Components

Server Components can import and render Client Components:
Client Components cannot import Server Components. Server Components must be passed as children or props.

Data Fetching

Direct database access

File system access

External API calls

Parallel data fetching

Streaming with Suspense

Server Components work seamlessly with Suspense for progressive rendering:

Multiple Suspense boundaries

Server Actions

Server Actions are async functions that run on the server and can be called from Client Components:

Defining Server Actions

Using Server Actions in forms

Programmatic Server Actions

Server Actions with validation

Patterns and Best Practices

Passing data from Server to Client Components

Composing Client and Server Components

Keeping Server-only code secure

Sharing code between Server and Client

Rendering Server Components

With renderToPipeableStream (Node.js)

With renderToReadableStream (Edge)

Common Patterns

Loading states

Error boundaries

Data caching

Common Issues

Server Components cannot use hooks like useState, useEffect, or useContext:
Functions (including event handlers) cannot be serialized:
Client Components cannot import Server Components:

See Also