π±οΈ Next.js 16: Client Components & 'use client' Directive
Pichle lesson mein humne dekha ki Server Components performance ke liye kitne zaroori hain. Par sawal ye aata hai: agar sab kuch server pe ho raha hai, toh user interaction (clicks, forms, animations) kaise honge? Yahin entry hoti hai Client Components ki.
π§ 1. Client Component Kya Hota Hai? (Basic Definition)
Client Component wo component hota hai jo browser mein run hota hai.
Execution
Iska JavaScript bundle browser download karta hai aur React ise clientβside par execute karta hai.
Interactivity
Bina Client Components ke aap:
- Buttons handle nahi kar sakte
- Forms submit nahi kar sakte
- Realβtime UI updates nahi kar sakte
"use client";
export default function InteractiveButton() {
return (
<button onClick={() => alert("Clicked!")}>Click Me</button>
);
}π οΈ 2. 'use client' Directive: The Signal
'use client' koi hook ya function nahi hai. Ye Next.js ke liye ek signal hai.
Kaam
Next.js ko batata hai:
"Is file aur iske saare children ko client bundle mein daal do."
Rule
- Ye file ke sabse top par hona chahiye
- Iske upar sirf comments allowed hote hain
- Imports hamesha iske niche aate hain
ποΈ 3. The Boundary Concept (Server β Client) π₯
App Router ka sabse powerful concept hai Client Boundary.
Definition
Jahan Serverβside rendering khatam hoti hai aur Clientβside execution start hota hai, wahi Client Boundary hai.
Flow
Jab aap Server Component ke andar Client Component import karte ho, boundary wahi se start ho jati hai.
π§© Chef & Waiter Analogy
- Server (Chef) β Kitchen mein HTML banata hai
- Client (Waiter) β Browser mein user se interact karta hai
- Boundary β Kitchen ka door jahan se plate handover hoti hai
β‘ 4. Hydration Kya Hoti Hai? (LifeβGiving Process)
Hydration wo process hai jo "dead HTML" ko interactive UI banata hai.
Steps
- Server se ready HTML browser mein aata hai β User ko content dikh jata hai
- Browser JavaScript download karta hai
- React event handlers attach karta hai β UI interactive ban jati hai
π§© Analogy
HTML = Body
JavaScript = Soul
Hydration = Body mein jaan aana
β οΈ 5. Common Mistakes: 'use client' Galat Jagah Lagana
β Root Level Clientization
Agar aapne layout.js ya page.js ke top par 'use client' likh diya:
- Puri app Clientβside SPA ban sakti hai
- Server Components ke fayde (SEO, speed) khatam ho jate hain
β Heavy Bundles
Jitne zyada Client Components β Utna bada JS bundle β Utni slow site
β
6. Kab 'use client' Likhna Chahiye?
Sirf tab jab aap ye use kar rahe ho:
- React Hooks β
useState,useEffect,useContext - Event handlers β
onClick,onSubmit,onChange - Browser APIs β
window,document,localStorage - Animation libraries β Framer Motion etc.
π 7. Gold Rule: Client Components Ko Niche Rakho
Performance optimize karne ka best tareeka:
Client Components ko hierarchy mein jitna niche ho sake utna niche rakho.
β Wrong Way (Whole Page Client)
"use client";
export default function BadPage() {
return (
<div>
<nav>Static Navbar</nav>
<main>Heavy Static Content</main>
<button onClick={() => {}}>Interactive Button</button>
</div>
);
}β Correct Way (Only Button Client)
// Page.js (Server Component)
import InteractiveButton from "./Button";
export default function GoodPage() {
return (
<div>
<nav>Static Navbar</nav>
<main>Heavy Static Content</main>
<InteractiveButton />
</div>
);
}π― Final Mental Model Summary
- Server Component (Default) β Data fetching + Static UI
- Client Component (Exception) β Interaction + State
- Boundary β Jahan interaction start hoti hai
- Hydration β HTML ko interactive banane ka bridge
π Next Topic
Server β Client Communication & Serialization Rules β kya hum client component mein server functions bhej sakte hain?