React Server Components: A New Paradigm
React Server Components enable server-side React. After building with RSC, here’s how to use them effectively.
What are Server Components?
Server Components:
- Run on server - Not in browser
- Zero bundle - No JavaScript sent
- Direct data access - Database queries
- Streaming - Progressive rendering
Basic Usage
Server Component
// app/users/page.tsx (Server Component by default)
async function UsersPage() {
const users = await db.users.findMany();
return (
<div>
{users.map(user => (
<UserCard key={user.id} user={user} />
))}
</div>
);
}
Client Component
'use client';
import { useState } from 'react';
export function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
}
Best Practices
- Default to server - Use server components
- Use client sparingly - Only when needed
- Pass serializable props - No functions
- Stream data - Progressive loading
- Optimize queries - Efficient data fetching
- Handle errors - Error boundaries
- Test thoroughly - Server and client
- Monitor - Track performance
Conclusion
React Server Components enable:
- Zero bundle size
- Direct data access
- Better performance
- Simpler architecture
Use server components by default, client when needed. The patterns shown here build efficient React applications.
React Server Components from July 2023, covering server-side React patterns.