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/awaitdirectly 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
- Server Component
- Client Component
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: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
Cannot use hooks in Server Components
Cannot use hooks in Server Components
Server Components cannot use hooks like
useState, useEffect, or useContext:Cannot pass functions to Client Components
Cannot pass functions to Client Components
Functions (including event handlers) cannot be serialized:
Cannot import Server Components in Client Components
Cannot import Server Components in Client Components
Client Components cannot import Server Components:
See Also
renderToPipeableStream- Render Server Components with Node.js streamsrenderToReadableStream- Render Server Components with Web Streams- Server Rendering Overview - Architecture and concepts