1. Revolusi React Server Components (RSC)
Secara default di Next.js App Router, **semua komponen adalah Server Components**. - **Server Component**: Dijalankan 100% di server, memiliki akses langsung ke database (`postgres.js`, file system `fs`, secret API keys), dan **kode JavaScript-nya tidak dikirimkan ke browser client** (0 KB bundle size overhead). - **Client Component** (`'use client'`): Dijalankan di browser, dibutuhkan jika komponen memerlukan interaktivitas (`useState`, `useEffect`, `onClick`, `onChange`, browser API seperti `localStorage` / `window`).
2. Kapan Harus Menggunakan 'use client'?
example.js
javascript
| 1 | // ✅ Server Component (Default - Langsung query database tanpa API route) |
| 2 | import { sql } from '@/lib/db'; |
| 3 | |
| 4 | export default async function CourseList() { |
| 5 | const courses = await sql`SELECT * FROM courses ORDER BY order_num ASC`; |
| 6 | |
| 7 | return ( |
| 8 | <div> |
| 9 | {courses.map(c => ( |
| 10 | <div key={c.id}>{c.title}</div> |
| 11 | ))} |
| 12 | {/* Client Component dimasukkan sebagai leaf node */} |
| 13 | <EnrollButton courseId={courses[0]?.id} /> |
| 14 | </div> |
| 15 | ); |
| 16 | } |
| 17 | |
| 18 | // ----------------------------------------------------- |
| 19 | // ✅ Client Component ('use client' diletakkan di baris pertama) |
| 20 | 'use client'; |
| 21 | import { useState } from 'react'; |
| 22 | |
| 23 | export function EnrollButton({ courseId }) { |
| 24 | const [loading, setLoading] = useState(false); |
| 25 | return ( |
| 26 | <button onClick={() => alert('Enrolled ' + courseId)}> |
| 27 | Ikuti Kursus |
| 28 | </button> |
| 29 | ); |
| 30 | } |
Pola Arsitektur: Push 'use client' to the Leaves
Pertahankan halaman utama dan layout sebagai Server Components untuk performa loading maksimal dan SEO optimal. Hanya tandai tombol kecil atau widget interaktif spesifik dengan `'use client'`.