React Server Components
RSC
React components rendered on the server. Reduces bundle size and allows direct DB access.
RSC
React components rendered on the server. Reduces bundle size and allows direct DB access.
React Server Components (RSC) is a new component paradigm developed by the React team, in which components run only on the server and only the rendering result is sent to the client. Unlike conventional SSR (Server-Side Rendering), RSC is not included in the JavaScript bundle, so the bundle size is dramatically reduced. If heavy libraries (moment.js, lodash, etc.) are used only in Server Components, they are not sent to the client.
A key feature of RSC is that async/await can be used directly in components. Server Components can directly access databases, file systems, and internal APIs, and fetch data without useEffect or React Query. This solves the Waterfall request problem and makes it possible to take advantage of the low latency between the server and the database.
Server Components and Client Components are clearly distinguished. Client Components must have the 'use client' directive added at the top of the file, and are used when useState, useEffect, event handlers, or browser APIs are needed. Server Components are the default, and they can include Client Components as children, but the reverse is not possible (passing them as props is possible).
The Next.js 13+ App Router is the representative implementation of RSC. page.tsx, layout.tsx, loading.tsx, and error.tsx work as Server Components by default, and combined with Streaming SSR, they enable progressive rendering. React 19 also introduced Server Actions, so form submissions and mutations can be handled directly on the server.
// app/posts/page.tsx - μλ² μ»΄ν¬λνΈ (κΈ°λ³Έκ°)
// 'use client' μμΌλ©΄ μλ² μ»΄ν¬λνΈ
import { prisma } from '@/lib/prisma';
import { formatDate } from 'date-fns'; // μ΄ λΌμ΄λΈλ¬λ¦¬λ λ²λ€μ ν¬ν¨ μ λ¨!
// async μ»΄ν¬λνΈ - μλ²μμλ§ κ°λ₯
export default async function PostsPage() {
// DB μ§μ μ κ·Ό (API λΆνμ)
const posts = await prisma.post.findMany({
include: { author: true },
orderBy: { createdAt: 'desc' },
take: 10,
});
// λ¬΄κ±°μ΄ λ°μ΄ν° μ²λ¦¬λ μλ²μμ
const processedPosts = posts.map(post => ({
...post,
excerpt: post.content.slice(0, 150),
formattedDate: formatDate(post.createdAt, 'yyyyλ
Mμ dμΌ'),
}));
return (
<main className="container mx-auto py-8">
<h1 className="text-3xl font-bold mb-6">μ΅μ κ²μκΈ</h1>
<div className="grid gap-4">
{processedPosts.map(post => (
<article key={post.id} className="p-6 border rounded-lg">
<h2 className="text-xl font-semibold">{post.title}</h2>
<p className="text-gray-600 mt-2">{post.excerpt}...</p>
<div className="mt-4 text-sm text-gray-500">
<span>{post.author.name}</span>
<span className="mx-2">β’</span>
<span>{post.formattedDate}</span>
</div>
</article>
))}
</div>
</main>
);
}
// λ©νλ°μ΄ν°λ μλ²μμ μμ±
export async function generateMetadata() {
const totalPosts = await prisma.post.count();
return {
title: `κ²μκΈ λͺ©λ‘ (${totalPosts}κ°)`,
description: 'μ΅μ κ²μκΈμ νμΈνμΈμ',
};
}
// app/posts/actions.ts - Server Actions
'use server';
import { prisma } from '@/lib/prisma';
import { revalidatePath } from 'next/cache';
export async function createPost(formData: FormData) {
const title = formData.get('title') as string;
const content = formData.get('content') as string;
await prisma.post.create({
data: { title, content, authorId: 1 }
});
// μΊμ 무ν¨ν - λͺ©λ‘ νμ΄μ§ κ°±μ
revalidatePath('/posts');
}
export async function deletePost(id: number) {
await prisma.post.delete({ where: { id } });
revalidatePath('/posts');
}
// -------------------------------------------------
// components/CreatePostForm.tsx - ν΄λΌμ΄μΈνΈ μ»΄ν¬λνΈ
'use client';
import { useFormStatus } from 'react-dom';
import { createPost } from '@/app/posts/actions';
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button
type="submit"
disabled={pending}
className="bg-blue-500 text-white px-4 py-2 rounded disabled:opacity-50"
>
{pending ? 'μ μ₯ μ€...' : 'κ²μκΈ μμ±'}
</button>
);
}
export function CreatePostForm() {
return (
<form action={createPost} className="space-y-4">
<input
name="title"
placeholder="μ λͺ©"
required
className="w-full border p-2 rounded"
/>
<textarea
name="content"
placeholder="λ΄μ©"
required
rows={5}
className="w-full border p-2 rounded"
/>
<SubmitButton />
</form>
);
}
// -------------------------------------------------
// app/posts/new/page.tsx - μλ² μ»΄ν¬λνΈμμ ν΄λΌμ΄μΈνΈ μ»΄ν¬λνΈ μ¬μ©
import { CreatePostForm } from '@/components/CreatePostForm';
export default function NewPostPage() {
return (
<main className="max-w-2xl mx-auto py-8">
<h1 className="text-2xl font-bold mb-6">μ κ²μκΈ μμ±</h1>
{/* ν΄λΌμ΄μΈνΈ μ»΄ν¬λνΈ μ½μ
*/}
<CreatePostForm />
</main>
);
}
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free