React Server Components(RSC)
5 min read
React Server Components 是 React 引入的最新架构升级之一,为开发者提供了构建更加高效、现代化 Web 应用的新方式。
React Server Components(简称 RSC)是运行在服务端的 React 组件。它们不会被打包进客户端 JS bundle,因此可以大大减小前端资源体积,提高首屏加载性能。
useState
、useEffect
等 React Hooks💡 非常适合展示数据、静态内容、无需交互的 UI 片段,例如文章详情页、列表页、SEO 页面等
React Server Components 并不是 SSR(Server-Side Rendering)或 SSG(Static Site Generation)的替代品,而是与它们配合使用的一种新的组件运行方式
以下是一张图示,帮助你理解 Server Component 到 Client 的数据流:
RSC 通常配合框架使用,例如 Next.js 13+ 默认启用 RSC 支持
app/
posts/
page.tsx ← Server Component
components/
post-card.tsx ← Client Component
lib/
db.ts ← 数据源访问
import { getPosts } from '@/lib/db'
import { PostCard } from '@/components/post-card'
export default async function PostsPage() {
const posts = await getPosts()
return (
<div className="space-y-4">
{posts.map((post) => (
<PostCard key={post.id} post={post} />
))}
</div>
)
}
'use client'
import * as React from 'react'
type Props = {
post: {
id: number
title: string
content: string
}
}
export function PostCard({ post }: Props) {
const [liked, setLiked] = React.useState(false)
return (
<div className="border p-4 rounded-md shadow">
<h2 className="font-bold text-xl">{post.title}</h2>
<p className="text-gray-700">{post.content}</p>
<button
onClick={() => setLiked(!liked)}
className="mt-2 text-sm text-blue-500"
>
{liked ? '💙 Liked' : '🤍 Like'}
</button>
</div>
)
}
特性 | Server Component | Client Component |
---|---|---|
执行位置 | 服务器 | 浏览器 |
是否打包到客户端 | ❌ | ✅ |
可访问服务器资源 | ✅ | ❌ |
使用状态/生命周期 | ❌ | ✅ |
使用场景 | 数据渲染、静态内容 | 表单、动画、交互逻辑 |
'use client'
,否则会失去 RSC 带来的优化React Server Components 是 React 构建现代 Web 应用的关键进化之一,它让我们可以更合理地拆分客户端和服务端的逻辑。
通过使用 RSC,我们可以:
未来,随着 Next.js 和其他框架的支持,RSC 将成为大型 Web 应用的基础架构之一。