Overview
React Server Components (RSC) are a new type of component that runs only on the server. They allow you to build components that fetch data, access backend resources, and render to HTML without sending JavaScript to the client.Server vs Client Components
Server Components
- Run only on the server
- Can access backend resources directly
- Don’t increase client bundle size
- Can’t use browser APIs or state
- Can’t use hooks like useState, useEffect
- Can be async functions
Client Components
- Run on both server (SSR) and client
- Can use all React features
- Include hooks and interactivity
- Increase bundle size
- Marked with
'use client'directive
Server Component API
Frompackages/react/src/ReactServer.js:43:
- No
useState,useReducer - No
useEffect,useLayoutEffect - No
useContext(use props instead) - No
useTransition,useDeferredValue
Basic Server Component
Client Component
Composing Server and Client Components
Server Component Using Client Component
Passing Server Components as Children
Data Fetching
Direct Database Access
API Calls
React Cache
Frompackages/react/src/ReactCacheServer.js, use cache to deduplicate requests:
Streaming with Suspense
Server components work seamlessly with Suspense for streaming:Server Actions
Server components can use server actions for mutations:Error Handling
Serialization Limits
Benefits
- Reduced Bundle Size: Server component code never goes to the client
- Direct Backend Access: Query databases, read files, use server-only APIs
- Improved Performance: Less JavaScript to download and parse
- Automatic Code Splitting: Each server component is a natural split point
- Better Security: Keep sensitive code and credentials on the server
Best Practices
-
Use server components by default: Only add
'use client'when needed - Push client boundaries down: Keep client components as deep in the tree as possible
- Pass server components as children: Compose server and client components effectively
-
Cache expensive operations: Use React
cachefor request deduplication - Use Suspense for streaming: Stream expensive components independently
- Handle errors properly: Use error boundaries with server components
- Avoid prop serialization: Don’t pass complex objects to client components
Limitations
- No hooks (except allowed ones): Can’t use useState, useEffect, etc.
- No browser APIs: window, document, etc. are not available
- No event handlers: onClick, onChange, etc. require client components
- No Context: Can’t use React Context (use props or composition)
- Framework dependency: Requires a supporting framework
When to Use Client Components
Use'use client' when you need:
- Interactivity (onClick, onChange, etc.)
- State management (useState, useReducer)
- Effects (useEffect, useLayoutEffect)
- Browser APIs (localStorage, WebSocket, etc.)
- Context (useContext)
- Lifecycle methods
Server Component Hooks
Allowed hooks in server components:useId: Generate unique IDsuseCallback: Memoize callbacksuseMemo: Memoize valuesuseDebugValue: Add debug labelscache: Cache function results
See Also
- Suspense - Streaming and loading states