-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-components-vs-client-components.js
More file actions
88 lines (77 loc) · 2.11 KB
/
server-components-vs-client-components.js
File metadata and controls
88 lines (77 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* 📘 Topic: Server Components vs Client Components in Next.js (13+)
*
* Next.js App Directory (`app/`) allows a mix of:
* ✅ Server Components (default)
* ✅ Client Components (opt-in via `"use client"`)
*
* Ye architecture performance, UX, aur dev experience dono mein improvement laata hai.
*/
/**
* 🔹 1. What is a Server Component?
* - Runs ONLY on the server
* - Never sent to browser (zero JS on client)
* - Can safely fetch data, access DB, secrets
* - Great for rendering heavy UI/data at build or request time
*
* Example:
*/
export default async function ServerComponent() {
const res = await fetch('https://api.example.com/posts', { cache: 'no-store' });
const data = await res.json();
return (
<div>
<h1>Server Rendered Posts</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
}
/**
* 🔹 2. What is a Client Component?
* - Runs in browser
* - Can use useState, useEffect, event handlers
* - Required for interactivity, animations, form handling, etc.
*
* ⚠️ Must be explicitly declared with `"use client"`
*/
"use client";
import { useState } from "react";
export default function ClientComponent() {
const [count, setCount] = useState(0);
return (
<div>
<p>Clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click Me</button>
</div>
);
}
/**
* 🔄 3. How to use both together:
* - Server components can render client components inside them.
* - But not vice versa. (Client can't render server)
*
* Good pattern:
* ServerComponent
* └── ClientComponent (nested interactivity)
*/
/**
* 🔍 4. When to use what:
*
* ✅ Use Server Components:
* - Fetching and rendering data
* - SEO critical content
* - Reducing client bundle size
*
* ✅ Use Client Components:
* - State and effects
* - User interaction
* - Dynamic rendering in browser
*/
/**
* 🧠 TL;DR:
* - Server Components = zero client JS, faster, data-safe
* - Client Components = browser logic, interactivity
* - Combine for best DX and UX
*
* 🔥 Tip: Start with server, only mark `"use client"` where needed
*/