Server Components का परिचय
In this page:
// Server Component (default): can be async, no hooks
async function ServerComponent() {
const data = await fetchData();
return <div>{data.value}</div>;
}
// Client Component: first line of the file
"use client";
function ClientComponent() {
const [state, setState] = useState(initial);
return <button onClick={() => setState(state + 1)}>{state}</button>;
}
Server Components क्या हैं
एक Server Component पूरी तरह server पर run होता है, इसका output (एक special serialized format के रूप में) render करते हुए और उसे directly browser को भेजते हुए, बजाय इसके कि JavaScript भेजे जो component को browser में run करे।
इसका मतलब है इसका code (और इसके कोई भी dependencies) client को कभी download करने की ज़रूरत ही नहीं।
उदाहरण: What Server Components Are
// Run in your local React project (npm install required)
// app/page.js (Next.js App Router — a Server Component by default)
async function getPosts() {
return [{ id: 1, title: 'Hello World' }];
}
export default async function Page() {
const posts = await getPosts();
return <ul>{posts.map(p => <li key={p.id}>{p.title}</li>)}</ul>;
}
Server Components Hooks उपयोग क्यों नहीं कर सकते
useState और useEffect जैसे Hooks उस behavior को manage करते हैं जो सिर्फ एक running browser में sense बनाता है — clicks पर react करना, state changes पर re-render होना।
चूंकि एक Server Component का code browser में बिल्कुल run ही नहीं होता, इन hooks के पास attach करने के लिए कुछ नहीं होता, इसलिए वहां इनकी simply अनुमति नहीं है।
Interactive Parts को Client Components के रूप में Mark करना
एक file की बहुत पहली line के रूप में 'use client' जोड़ना उस component (और वह जो भी import करता है) को एक Client Component मार्क करता है, जो hooks और event handlers उपयोग कर सकता है, और normally browser में run करने के लिए JavaScript के रूप में shipped होता है जैसे React हमेशा से करता है।
उदाहरण: Marking Interactive Parts as Client Components
// Run in your local React project (npm install required)
'use client';
export default function LikeButton() {
const [liked, setLiked] = useState(false);
return <button onClick={() => setLiked(!liked)}>{liked ? 'Liked' : 'Like'}</button>;
}
- एक Server Component के अंदर useState या useEffect उपयोग करने की कोशिश करना — hooks सिर्फ Client Components में काम करते हैं।
- यह मान लेना कि Server Components पूरी तरह Client Components replace कर देते हैं — ज़्यादातर real apps दोनों का mix उपयोग करते हैं।
- किसी file के top पर एक 'use client' directive भूल जाना जिसे interactivity चाहिए, इसे (गलत तरीके से) server-only treated रखते हुए।
- Server Components server पर render होते हैं, browser को finished HTML/output भेजते हुए बजाय वहां run होने वाले JS के।
- वे hooks, browser APIs, या event handlers उपयोग नहीं कर सकते — उनके लिए Client Components चाहिए।
- किसी file के top पर एक 'use client' directive इसे (और इसके children को) एक Client Component मार्क करता है।
- यह model primarily Next.js जैसे frameworks के through उपयोग होता है, plain client-side React apps में नहीं।
Server Components support वाला एक framework चाहिए (Next.js 13+ App Router); यह एक plain client-side React feature नहीं है और इस preview sandbox में run नहीं हो सकता।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Error Boundaries
- React Portals
- Modals using Portals (practical use)
- React Suspense
- Code Splitting with React.lazy
- Introduction to Server Components
- Introduction to Next.js (server-side React)
- Using React with TypeScript
- Scalable Folder Architecture
- Common React Design Patterns
- Component Documentation with Storybook
- Accessibility (a11y) in React
- i18n with react-i18next
- React Security Best Practices