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

404 Pages को Handle करना

एक 404 page एक locked door पर छोड़े गए friendly note जैसा है जो कहता है 'यह room exist नहीं करता' visitors को एक blank hallway में घूरते हुए छोड़ने के बजाय।
Syntax
markup
<Routes>
  <Route path="/" element={<Home />} />
  <Route path="*" element={<NotFound />} />
</Routes>

एक Catch-All Route जोड़ना

path='*' के साथ defined एक Route literally किसी भी URL को match करता है जिसे पहले वाले किसी route ने already match नहीं किया। इसे अपने Routes block के अंदर last Route रखना unmatched URLs को blank page दिखने के बजाय land करने के लिए कहीं देता है।

Note: Development के दौरान एक made-up URL जैसे /this-does-not-exist पर visit करके अपना 404 page test करें।
Warning: आपके असली routes से पहले रखा गया एक wildcard route सब कुछ पहले match कर देगा, आपके किसी भी असली pages को कभी render होने से रोकते हुए।

उदाहरण: Adding a Catch-All Route

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 <h1>404 - Page Not Found</h1>; }

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.

404 Page को Helpful बनाना

एक अच्छा 404 page 'not found' कहने से ज़्यादा करता है — यह user को reassure करता है और इन्हें recover करने का एक obvious तरीका देता है, आमतौर पर homepage या एक search box का एक link। यह एक dead end को एक छोटे, graceful redirect में बदल देता है।

Note: Home वापस जाने के लिए एक Link (plain <a> नहीं) शामिल करें, ताकि एक 404 से recover करना एक smooth client-side navigation रहे।
Warning: कोई explanation या बाहर जाने का तरीका न होने वाला एक bare 404 एक poor experience है — हमेशा user को इनका next step दें।

उदाहरण: Making the 404 Page Helpful

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

function NotFound() {
  return (
    <div>
      <h1>404 - Page Not Found</h1>
      <p>Sorry, that page doesn't exist.</p>
      <Link to="/">Go back home</Link>
    </div>
  );
}

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

एक Specific Section के लिए Nested 404s

वही wildcard pattern nested routes के अंदर भी काम करता है, आपके app के एक specific section (जैसे /dashboard/*) को इसका खुद का contextual 'not found' message दिखाने देते हुए app-wide वाले के बजाय, जबकि दूसरे sections अब भी global 404 उपयोग करते हैं।

Note: एक section-specific 404 section के shared layout (जैसे एक sidebar) को visible रख सकता है, जो global 404 आमतौर पर नहीं रखता।
Warning: एक global और कई nested wildcard routes दोनों रखना confusing हो सकता है — clearly document करें कि कौन सा कौन से cases handle करता है।

उदाहरण: Nested 404s for a Specific Section

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

function DashboardLayout() { return <div><nav>Dashboard Nav</nav><Outlet /></div>; }
function DashboardNotFound() { return <p>No such dashboard page</p>; }

function AppRoutes() {
  return (
    <Routes>
      <Route path="/dashboard" element={<DashboardLayout />}>
        <Route path="*" element={<DashboardNotFound />} />
      </Route>
    </Routes>
  );
}

⚠️ 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. बिलकुल wildcard route जोड़ना भूल जाना, unmatched URLs को पूरी तरह blank render होने देते हुए।
  2. Wildcard route को last के अलावा कहीं और रखना, इसे accidentally दूसरे routes swallow करने देते हुए।
  3. 404 page को safety वापस जाने का कोई तरीका न देना, जैसे home page का एक link।
चैप्टर सारांश
  • path='*' वाला एक Route किसी भी URL को match करता है जिसे पहले वाले, ज़्यादा specific route ने match नहीं किया।
  • यह Routes के अंदर last Route होना चाहिए ताकि यह असली pages को shadow न करे।
  • एक अच्छा 404 page बताता है कि क्या हुआ और एक known-good page पर वापस जाने का link offer करता है।
  • यह pattern nested routes के लिए भी काम करता है, एक specific section के अंदर unmatched paths catch करते हुए।
ब्राउज़र सपोर्ट

npm install react-router-dom चाहिए — इस 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.