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

Outlet Component की जानकारी

Outlet wall में built-in एक picture frame जैसा है — यह खाली रहता है जब तक आप जो भी picture (child page) currently belong करे उसे hang न करें।
Syntax
markup
function ParentLayout() {
  return (
    <div>
      <header>Shared layout</header>
      <Outlet context={sharedValue} />
    </div>
  );
}

Outlet असल में क्या करता है

Outlet एक special component है जो parent route के rendered output के अंदर एक placeholder की तरह act करता है। JSX में जहां <Outlet /> appear होता है, React Router उस जगह वह child route element substitute कर देता है जो currently URL के बाकी हिस्से से match करता है।

Note: Outlet को 'यहां current page जाता है' की तरह सोचें एक shared layout component के अंदर।
Warning: Outlet सिर्फ react-router-dom के nested routing system से आता है — इसका एक component के खुद के children prop से कोई लेना-देना नहीं है।

उदाहरण: What Outlet Actually Does

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

function Layout() {
  return <div><header>Site Header</header><Outlet /></div>;
}
function Page() { return <p>A page inside the layout</p>; }

function AppRoutes() {
  return (
    <Routes>
      <Route path="/" element={<Layout />}>
        <Route path="page" element={<Page />} />
      </Route>
    </Routes>
  );
}

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

Matching Child के बिना Outlet कुछ नहीं Render करता

अगर current URL parent route से match करे लेकिन इसके अंदर कोई child route match न करे (उदाहरण के लिए, exactly /dashboard पर visit करना जब सिर्फ /dashboard/settings एक child के रूप में defined हो), Outlet बिलकुल कुछ render नहीं करता — surrounding layout अब भी दिखता है, सिर्फ एक empty content area के साथ।

Note: एक index route को default child के रूप में जोड़ें parent के bare path के लिए कभी empty Outlet न दिखाने के लिए।
Warning: एक empty Outlet एक bug (blank content area) जैसा लग सकता है जब असल में इसका मतलब सिर्फ है कोई child route match नहीं हुआ — अपने route definitions check करें।

उदाहरण: Outlet Renders Nothing Without a Matching Child

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

function Layout() { return <div><nav>Nav</nav><Outlet /></div>; }
function Home() { return <p>Default home content</p>; }

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

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

context के साथ Outlet को Data Pass करना

Outlet एक optional context prop accept करता है, एक parent layout को data नीचे pass करने देते हुए जो भी child route currently render हो उसे। Child इसे वापस useOutletContext() hook के साथ पढ़ता है, इसके लिए अलग से React Context setup करने की ज़रूरत से बचते हुए।

Note: यह data (जैसे current user) share करने के लिए handy है जो हर nested page को चाहिए, हर एक में एक fetch duplicate किए बिना।
Warning: useOutletContext() सिर्फ उस specific Outlet के child के रूप में rendered components के लिए काम करता है — यह कहीं और unrelated components में काम नहीं करेगा।

उदाहरण: Passing Data to Outlet with context

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

function Layout() {
  return <Outlet context={{ user: "Asha" }} />;
}
function Child() {
  const { user } = useOutletContext();
  return <p>Hello, {user}</p>;
}

⚠️ 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. Outlet को children के साथ confuse करना — Outlet specifically nested ROUTES के लिए है, regular component composition के लिए नहीं।
  2. यह भूल जाना कि Outlet सिर्फ तब कुछ render करता है जब current URL के लिए actually-matching child route हो।
  3. एक layout में multiple <Outlet /> instances रखना हर एक में अलग content expect करते हुए — सिर्फ एक Outlet matched child render करता है।
चैप्टर सारांश
  • Outlet react-router-dom का एक component है जो एक parent route के element के अंदर उपयोग होता है।
  • यह जो भी child route currently URL से match करे उसे, उसी जगह, render करता है।
  • अगर कोई child route match न करे, Outlet कुछ नहीं render करता।
  • यह वह mechanism है जो nested routing और shared layouts को possible बनाता है।
ब्राउज़र सपोर्ट

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.