← Back to React Course | Chapter 8: Routing | Lesson 2 of 10

Routes Define करना

Routes define करना एक map बनाने जैसा है जो कहता है 'अगर कोई इस address के लिए पूछे, तो उन्हें यह room दिखाओ'।
Syntax
markup
<Routes>
  <Route path="/" element={<Home />} />
  <Route path="/path" element={<Page />} />
  <Route path="*" element={<NotFound />} />
</Routes>

Routes और Route का Relationship

<Routes> एक switchboard की तरह act करता है: यह current URL देखता है और इसके अंदर सिर्फ वह single <Route> render करता है जिसका path match करता है। हर <Route> एक URL path को उस component (element prop के through) के साथ pair करता है जो उस path के लिए दिखाया जाना चाहिए।

Note: v6 में order v5 से कम matter करता है क्योंकि v6 automatically best match चुनता है, लेकिन general paths से पहले specific paths रखना अब भी good practice है।
Warning: React Router v6 में, हर <Route> को element prop चाहिए — v5 का पुराना component prop अब काम नहीं करता।

उदाहरण: The Routes and Route Relationship

markup
// Run in your local React project (npm install required)
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function Home() { return <p>Home</p>; }
function Contact() { return <p>Contact</p>; }

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/contact" element={<Contact />} />
      </Routes>
    </BrowserRouter>
  );
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Wildcard से Unmatched Routes Catch करना

एक <Route path='*'> किसी भी URL को match करता है जो पहले वाले, ज़्यादा specific route से match नहीं हुआ। इसे <Routes> के अंदर last रखना इसे आपके app के explicitly handle न किए हुए किसी भी address के लिए एक catch-all 404 page की तरह act करवाता है।

Note: 404 route को एक helpful message और home वापस जाने का एक link दें, एक bare 'not found' text के बजाय।
Warning: अगर wildcard route last के बजाय FIRST रखी जाए, तो यह सब कुछ match कर सकता है और आपके दूसरे, ज़्यादा specific routes को कभी reach होने से रोक सकता है।

उदाहरण: Catching Unmatched Routes with a Wildcard

markup
// Run in your local React project (npm install required)
import { Routes, Route } from 'react-router-dom';

function Home() { return <p>Home</p>; }
function NotFound() { return <p>404 - Not found</p>; }

function AppRoutes() {
  return (
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="*" element={<NotFound />} />
    </Routes>
  );
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

एक ही Layout के लिए Multiple Routes Render करना

कई अलग paths अलग top-level pages render कर सकते हैं फिर भी same overall app shell (जैसे एक fixed navigation bar) share करते हुए अगर वह shell <Routes> block के बाहर रखा गया हो।

सिर्फ एक specific Route से matched हिस्सा swap होता है जब URL बदलता है।

Note: Persistent UI (navbar, footer) को <Routes> के बाहर रखें, और सिर्फ page-specific content individual routes' elements के अंदर रखें।
Warning: हर route के element component के अंदर navbar JSX duplicate करने का मतलब है यह हर navigation पर re-mount होता है (और कोई local state खोता है)।

उदाहरण: Rendering Multiple Routes for the Same Layout

markup
// Run in your local React project (npm install required)
import { Routes, Route } from 'react-router-dom';

function Nav() { return <nav>My Site</nav>; }
function Home() { return <p>Home</p>; }
function About() { return <p>About</p>; }

function App() {
  return (
    <div>
      <Nav />
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </div>
  );
}

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. <Route> पर element prop भूल जाना, जो React Router v6 में required हो गया (v5 के बजाय component उपयोग करता था)।
  2. Routes को ऐसे order करना कि एक general path accidentally एक ज़्यादा specific एक से पहले match हो जाए।
  3. <Route> elements को एक <Routes> parent के अंदर wrap न करना — React Router v5 code से एक common leftover।
चैप्टर सारांश
  • <Routes> एक container है जो current URL से match करने वाला पहला <Route> render करता है।
  • हर <Route> एक path string को उस element से map करता है जो इसके लिए render होना चाहिए।
  • React Router v6 element prop (element={<Home />}) उपयोग करता है v5 के component prop के बजाय।
  • '*' का एक path commonly unmatched routes (404 pages) के लिए catch-all के रूप में उपयोग होता है।
ब्राउज़र सपोर्ट

npm install react-router-dom (v6 syntax दिखाई गई) चाहिए — इस sandbox में CDN से available नहीं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.