๐ ๋น ๋ฅธ ํ (Hooks) & App Router ๋ ํผ๋ฐ์ค๊ฐ ํ์ํ์ ๊ฐ์?
์ค๋ฌด API ์กฐํ๋ฅผ ์ํ React & Next.js ์นํธ์ํธ ๋ฐ๋ก๊ฐ๊ธฐ โ
0. ๐ React 19 & Next.js App Router, ๋ฌด์์ด ๋ฌ๋ผ์ก๋์?
๊ธฐ์กด React(SPA)๋ ๋ธ๋ผ์ฐ์ ๊ฐ ๋น HTML๊ณผ ๊ฑฐ๋ํ JS ๋ฒ๋ค์ ๋ด๋ ค๋ฐ์ ๋ค์์ผ ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ๊ธฐ ๋๋ฌธ์ ์ด๊ธฐ ๋ก๋ฉ์ด ๋๋ ธ์ต๋๋ค. Next.js App Router + React 19๋ ๊ธฐ๋ณธ์ ์ผ๋ก **์๋ฒ ์ปดํฌ๋ํธ(RSC)**์์ DB/API ๋ฐ์ดํฐ๋ฅผ ์ง์ ์กฐํํด ์์ฑ๋ HTML์ ๋ด๋ ค๋ณด๋ด๊ณ , ๋ฒํผ ํด๋ฆญ ๊ฐ์ ์ํธ์์ฉ์ด ํ์ํ ๋ถ๋ถ๋ง ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๋ก ์ชผ๊ฐ์ด ๋ฒ๋ค ํฌ๊ธฐ๋ฅผ ๊ทน์ ์ผ๋ก ์ค์ ๋๋ค.
| ํต์ฌ ๊ฐ๋ | ์ผ์ ๋น์ | ์ฌ์ด ์ค๋ช |
|---|---|---|
| Server Component (RSC) | ์ฃผ๋ฐฉ์์ ์์ฑํด ๋์ค๋ ์๋ฆฌ | ์๋ฒ์์๋ง ์คํ๋์ด DB์ ์ง์ ์ ๊ทผ ๊ฐ๋ฅ, ๋ธ๋ผ์ฐ์ ๋ก JS ์ฝ๋๋ฅผ ๋ณด๋ด์ง ์์ (๊ธฐ๋ณธ๊ฐ) |
Client Component ('use client') | ํ ์ด๋ธ ์์์ ์ง์ ๊ตฝ๋ ์ค๋ธ์ค๋ธ | useState, onClick ๋ฑ ๋ธ๋ผ์ฐ์ ์ด๋ฒคํธยท์ํ๊ฐ ๊ผญ ํ์ํ UI ์กฐ๊ฐ์๋ง ์ ์ธ |
Server Action ('use server') | ๋ฐฑ์๋ API ์๋ํฌ์ธํธ ์๋ ํผ ์ ์ถ | ๋ณ๋ REST API ๋ผ์ฐํธ๋ฅผ ๋ง๋ค์ง ์๊ณ React <form action={...}>์์ ์๋ฒ ํจ์๋ฅผ ์ง์ ํธ์ถ |
Suspense ์คํธ๋ฆฌ๋ฐ | ์ํผํ์ด์ ๋จผ์ , ์คํ ์ดํฌ๋ ๋์ค์ | ๋น ๋ฅธ ํค๋/๋ฉ๋ด๋ 0.1์ด ๋ง์ ๋จผ์ ๋ณด์ฌ์ฃผ๊ณ , ๋๋ฆฐ ๋ฐ์ดํฐ ์์ญ๋ง ์ค์ผ๋ ํค ํ ์คํธ๋ฆฌ๋ฐ ๊ต์ฒด |
1. ํ์ ์ฌ์ ์๊ฑด & ์์คํ ์๊ตฌ์ฌ์ (Prerequisites)
๐ป ์์คํ ์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ๋น๊ณ |
|---|---|---|---|
| ์ด์์ฒด์ (OS) | Windows 10/11, macOS 13+, Linux | Windows 11, macOS (Apple Silicon), Ubuntu 22.04+ | Node.js ๊ตฌ๋ ๊ฐ๋ฅ 64-bit OS |
| CPU | 2 ์ฝ์ด ์ด์ | 4 ์ฝ์ด ์ด์ | Turbopack ๊ฐ๋ฐ ์๋ฒ ๋ฐ SSR ๋น๋ ๊ฐ์ |
| ๋ฉ๋ชจ๋ฆฌ (RAM) | 4 GB ์ด์ | 8 GB ~ 16 GB ์ด์ | Next.js ํ๋ก๋์ ๋น๋ ๋ฐ TypeScript ํ์ ์ฒดํฌ |
| ๋์คํฌ (Storage) | 2 GB ์ด์์ ์ฌ์ ๊ณต๊ฐ | 10 GB ์ด์์ ๊ณ ์ SSD | node_modules ๋ฐ .next ๋น๋ ์บ์ |
| ๋ฐํ์ | Node.js 20.x+ LTS | Node.js 22.x LTS + pnpm (๋๋ npm) | React 19 + Next.js 15+ ๊ธฐ์ค |
# ์ต์ Next.js + React 19 + TypeScript + Tailwind CSS ํ๋ก์ ํธ ์์ฑ
npx create-next-app@latest my-fullstack-app --ts --tailwind --eslint --app --src-dir
cd my-fullstack-app
npm install zod
npm run dev
2. ํ์ต ๋ก๋๋งต (5๋จ๊ณ)
flowchart TD
p1["[1๊ฐ] App Router ํ์ผ ๊ท์ฝ & ๋ ์ด์์<br/>page.tsx, layout.tsx, loading.tsx, ๋์ ๋ผ์ฐํธ [id]"]
p2["[2๊ฐ] Server vs Client ์ปดํฌ๋ํธ ๋ถ๋ฆฌ ์ ๋ต<br/>DB ์ง์ ์กฐํ(RSC) + ์ต์ ์ธํฐ๋ํฐ๋ธ ์ฌ('use client')"]
p3["[3๊ฐ] Server Actions & Zod ์ ํจ์ฑ ๊ฒ์ฆ<br/>React 19 useActionState๋ก ํ์คํ ํผ ๋ฎคํ
์ด์
๊ตฌํ"]
p4["[4๊ฐ] Suspense ์คํธ๋ฆฌ๋ฐ & ์บ์ฑ ์ ๋ต<br/>๋ณ๋ ฌ ํ์นญ(Promise.all)๊ณผ revalidatePath ์ฆ์ ๊ฐฑ์ "]
p5["[5๊ฐ] ํ๋ก๋์
๋ณด์, SEO & ๋ฐฐํฌ ์ฒดํฌ๋ฆฌ์คํธ<br/>generateMetadata, Server-only ๋ณดํธ ๋ฐ Docker/Vercel ๋ฐฐํฌ"]
p1 --> p2 --> p3 --> p4 --> p5
3. [1๊ฐ] App Router ํ์ผ ๊ธฐ๋ฐ ๋ผ์ฐํ
& ๋น๋๊ธฐ params
Next.js 15+๋ถํฐ ๋์ ๋ผ์ฐํธ์ params์ searchParams๋ **๋น๋๊ธฐ Promise**๋ก ์ ๋ฌ๋ฉ๋๋ค.
// src/app/posts/[id]/page.tsx (๊ธฐ๋ณธ๊ฐ์ด Server Component!)
import { notFound } from 'next/navigation';
interface PageProps {
params: Promise<{ id: string }>;
}
export default async function PostDetailPage({ params }: PageProps) {
// Next.js 15+ ํ์ค: params๋ฅผ ๋ฐ๋์ await ํด์ผ ํจ
const { id } = await params;
const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`, {
next: { revalidate: 60 }, // ISR: 60์ด๋ง๋ค ๋ฐฑ๊ทธ๋ผ์ด๋ ์บ์ ๊ฐฑ์
});
if (!res.ok) notFound();
const post: { id: number; title: string; body: string } = await res.json();
return (
<article className="max-w-2xl mx-auto p-6 space-y-4">
<span className="text-xs font-mono text-emerald-600">POST #{post.id}</span>
<h1 className="text-2xl font-bold">{post.title}</h1>
<p className="text-gray-600 leading-relaxed">{post.body}</p>
</article>
);
}
4. [2๊ฐ] Server Component + Client Component ์กฐํฉ ํจํด
์ ์ฒด ํ์ด์ง๋ฅผ 'use client'๋ก ๋ง๋ค๋ฉด SSR ์ด์ ์ ์์ต๋๋ค. ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ๊ป๋ฐ๊ธฐ๋ Server Component, ํด๋ฆญยท์ํ๊ฐ ํ์ํ ๋ฒํผ๋ง Client Component๋ก ๋ถ๋ฆฌํ์ธ์.
// src/components/LikeButton.tsx
'use client'; // ๋ธ๋ผ์ฐ์ ์ํธ์์ฉ์ด ํ์ํ ์ต์ ๋จ์!
import { useState } from 'react';
export function LikeButton({ initialCount }: { initialCount: number }) {
const [likes, setLikes] = useState(initialCount);
return (
<button
type="button"
onClick={() => setLikes((c) => c + 1)}
className="px-4 py-2 rounded-lg bg-emerald-600 text-white font-semibold"
>
๐ ์ข์์ {likes}
</button>
);
}
5. [3๊ฐ] React 19 useActionState + Server Actions + Zod ๊ฒ์ฆ
๋ณ๋์ /api/todos REST ์๋ํฌ์ธํธ ์์ด๋ ํ์
์์ ํ ํ์คํ ํผ ์ ์ถ๊ณผ ์๋ ํ๋ฉด ๊ฐฑ์ (revalidatePath)์ ๊ตฌํํ ์ ์์ต๋๋ค.
// src/app/todos/actions.ts
'use server';
import { z } from 'zod';
import { revalidatePath } from 'next/cache';
const TodoSchema = z.object({
title: z.string().min(2, 'ํ ์ผ์ 2๊ธ์ ์ด์ ์
๋ ฅํด์ผ ํฉ๋๋ค.').max(100),
});
export type ActionState = { error?: string; success?: boolean };
export async function createTodoAction(
_prevState: ActionState,
formData: FormData
): Promise<ActionState> {
const parsed = TodoSchema.safeParse({ title: formData.get('title') });
if (!parsed.success) {
return { error: parsed.error.issues[0].message };
}
// ์ค๋ฌด์์๋ ์ฌ๊ธฐ์ Prisma / Drizzle DB INSERT ์ํ
console.log('DB ์ ์ฅ ์๋ฃ:', parsed.data.title);
// /todos ํ์ด์ง ์บ์๋ฅผ ์ฆ์ ๋ฌดํจํํด ์ ๋ชฉ๋ก ๋ฐ์!
revalidatePath('/todos');
return { success: true };
}
// src/components/TodoForm.tsx
'use client';
import { useActionState } from 'react'; // React 19 ํ์ค ํ
!
import { createTodoAction, type ActionState } from '@/app/todos/actions';
const initialState: ActionState = {};
export function TodoForm() {
const [state, formAction, isPending] = useActionState(createTodoAction, initialState);
return (
<form action={formAction} className="flex flex-col gap-2">
<div className="flex gap-2">
<input
name="title"
placeholder="์๋ก์ด ํ ์ผ์ ์
๋ ฅํ์ธ์..."
className="border rounded px-3 py-2 flex-1"
disabled={isPending}
/>
<button
type="submit"
disabled={isPending}
className="px-4 py-2 bg-emerald-600 text-white rounded font-semibold disabled:opacity-50"
>
{isPending ? '์ ์ฅ ์ค...' : '์ถ๊ฐ'}
</button>
</div>
{state.error && <p className="text-sm text-red-500">{state.error}</p>}
{state.success && <p className="text-sm text-emerald-600">์ฑ๊ณต์ ์ผ๋ก ์ถ๊ฐ๋์์ต๋๋ค!</p>}
</form>
);
}
6. [4๊ฐ] Suspense ์คํธ๋ฆฌ๋ฐ & ๋ณ๋ ฌ ๋ฐ์ดํฐ ํ์นญ
๋๋ฆฐ AI ์ถ์ฒ ์์ ฏ ๋๋ฌธ์ ์ ์ฒด ํ์ด์ง๊ฐ 3์ด ๋์ ํ์๊ฒ ๋ฉ์ถ๋ ๊ฒ์ ๋ง๊ธฐ ์ํด **<Suspense>**๋ก ์์ญ์ ๊ฒฉ๋ฆฌํฉ๋๋ค.
// src/app/dashboard/page.tsx
import { Suspense } from 'react';
async function SlowAnalyticsPanel() {
await new Promise((r) => setTimeout(r, 1500)); // 1.5์ด ๊ฑธ๋ฆฌ๋ ๋ฌด๊ฑฐ์ด ์ฟผ๋ฆฌ ์๋ฎฌ๋ ์ด์
return <div className="p-4 border rounded">๐ ์ค์๊ฐ ๋งค์ถ ๋ถ์ ์๋ฃ: โฉ12,450,000</div>;
}
export default function DashboardPage() {
return (
<main className="p-6 space-y-6">
<h1 className="text-2xl font-bold">๊ด๋ฆฌ์ ๋์๋ณด๋ (์ฆ์ ๋ ๋๋ง๋จ!)</h1>
<Suspense fallback={<div className="p-4 border rounded animate-pulse">๋ถ์ ๋ฐ์ดํฐ ๋ก๋ฉ ์ค...</div>}>
<SlowAnalyticsPanel />
</Suspense>
</main>
);
}
7. [5๊ฐ] ํ๋ก๋์ ์ค์ ์ฒดํฌ๋ฆฌ์คํธ
- ๋ฐ์ดํฐ ํ์นญ ์ํฐํด ๋ฐฉ์ง (
Promise.all):- ์๋ก ์์กด์ฑ์ด ์๋ ๋ API(
getUser,getNotifications)๋ฅผawait๋ก ์ง๋ ฌ ํธ์ถํ์ง ๋ง๊ณconst [user, noti] = await Promise.all([...])๋ก ๋ณ๋ ฌ ํธ์ถํ์ธ์.
- ์๋ก ์์กด์ฑ์ด ์๋ ๋ API(
- Server Actions ๋ณด์ ์ธ์ฆ ํ์:
- Server Action์ ๋ด๋ถ์ ์ผ๋ก ๊ณต๊ฐ POST ์๋ํฌ์ธํธ์
๋๋ค. ํจ์ ์ต์๋จ์์ ๋ฐ๋์ ์ธ์
/๊ถํ ๊ฒ์ฆ(
auth())๊ณผZod์คํค๋ง ๊ฒ์ฆ์ ์ํํ์ธ์.
- Server Action์ ๋ด๋ถ์ ์ผ๋ก ๊ณต๊ฐ POST ์๋ํฌ์ธํธ์
๋๋ค. ํจ์ ์ต์๋จ์์ ๋ฐ๋์ ์ธ์
/๊ถํ ๊ฒ์ฆ(
- ํด๋ผ์ด์ธํธ ๋ฒ๋ค ๋์ถ ๋ฐฉ์ง (
server-only):- DB ํด๋ผ์ด์ธํธ๋ ๋น๋ฐ ํค ์ ํธ ํ์ผ ์๋จ์
import 'server-only';๋ฅผ ์ ์ธํ๋ฉด ์ค์๋ก Client Component์์ importํ ๋ ๋น๋ ํ์์ ์ฆ์ ์ฐจ๋จ๋ฉ๋๋ค.
- DB ํด๋ผ์ด์ธํธ๋ ๋น๋ฐ ํค ์ ํธ ํ์ผ ์๋จ์
- ์ด๋ฏธ์ง ์ต์ ํ (
next/image):- ์ผ๋ฐ
<img>๋์<Image>์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํด WebP/AVIF ์๋ ๋ณํ ๋ฐ CLS(๋ ์ด์์ ๋ฐ๋ฆผ)๋ฅผ ๋ฐฉ์งํ์ธ์.
- ์ผ๋ฐ
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์