Next.js का परिचय (server-side React)
In this page:
// app/route-name/page.js maps to the URL /route-name
export default function Page() {
return <h1>Page content</h1>;
}
// app/route-name/[id]/page.js maps to /route-name/123
React के ऊपर एक Framework क्यों Use करें
Plain React (Vite या Create React App के through) सिर्फ component-rendering layer handle करता है — अगर ज़रूरत हो तो आपको खुद routing, data fetching patterns, और server-rendering जोड़नी होती है।
Next.js इन सबको sensible defaults के साथ एक साथ bundle करता है, आपको full production apps ज़्यादा तेज़ी से build करने देते हुए।
उदाहरण: Why Use a Framework on Top of React
// Run in your local React project (npm install required)
// npx create-next-app@latest my-app
// cd my-app && npm run dev
// app/page.js
export default function HomePage() {
return <h1>Welcome to my Next.js app</h1>;
}
File-Based Routing
Routes manually define करने के बजाय (React Router के जैसे), Next.js का App Router एक page का URL directly इसकी file location से determine करता है: app/page.js homepage है, app/about/page.js /about बन जाता है, और इसी तरह — किसी separate route configuration file की ज़रूरत नहीं।
उदाहरण: File-Based Routing
// Run in your local React project (npm install required)
// File structure -> resulting routes:
// app/page.js -> /
// app/about/page.js -> /about
// app/blog/[slug]/page.js -> /blog/:slug (dynamic)
// app/about/page.js
export default function AboutPage() {
return <p>About us</p>;
}
Default रूप से Server-Side Rendering
Next.js के App Router में pages default रूप से Server Components हैं, मतलब वे server पर render होते हैं और browser को finished output भेजते हैं — SEO और fast initial loads के लिए अच्छा।
Interactivity selectively इसके अंदर nested 'use client' components के through जोड़ी जाती है।
उदाहरण: Server-Side Rendering by Default
// Run in your local React project (npm install required)
// app/page.js -- runs on the server, can directly await data
async function getData() {
return { title: 'Server-rendered title' };
}
export default async function Page() {
const data = await getData();
return <h1>{data.title}</h1>;
}
- Next.js को सिर्फ 'extra CSS वाला React' मानना — यह fundamentally rendering (server-side by default) और routing (file-based) को plain client-side React app की तुलना में बदल देता है।
- Client-only code (जैसे directly window access करना) को एक Server Component में बिना 'use client' directive के डालना।
- File-based routing न समझना — app/ या pages/ folder के under किसी file की location directly इसका URL determine करती है।
- Next.js एक popular React framework है जो React के ऊपर server-side rendering, file-based routing, और भी ज़्यादा जोड़ता है।
- App Router pages को folder structure के through एक 'app/' directory के under organize करता है; हर folder का एक page.js file हो सकता है।
- App Router में Server Components default हैं; interactive pieces के लिए 'use client' जोड़ें।
- Next.js build/deployment tooling (bundling, routing, SSR) का ज़्यादातर हिस्सा handle करता है जो एक plain React app में separately configure करना पड़ता।
npm install next react react-dom चाहिए (typically create-next-app के through) — इस sandbox में CDN के through available नहीं।
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