TIL | React Router Dom Outlet

๐Ÿš€ยท2022๋…„ 5์›” 24์ผ
0

์ด๋ฒˆ์ฃผ ๊ธฐ์—…๊ณผ์ œ๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์‹œ์ž‘ํ•˜๋ฉด์„œ ์—ญํ• ์„ ๋ถ„๋‹ดํ–ˆ๋‹ค.
๋‚˜๋Š” ์ดˆ๊ธฐ ๋ ˆ์ด์•„์›ƒ์„ ์งœ๊ณ , ๊ด‘๊ณ ๊ด€๋ฆฌ ํŽ˜์ด์ง€๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค
์ดˆ๊ธฐ ๋ ˆ์ด์•„์›ƒ์„ ์งœ๋ฉด์„œ ํŒ€์›๋ถ„์ด ์•Œ๋ ค์ฃผ์‹  react router dom์˜ outlet์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค!

v6์— ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€๋œ Outlet์€ react์˜ children๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์ž‘๋™ํ•จ์œผ๋กœ์„œ, ๋ ˆ์ด์•„์›ƒ ์„ค์ •์„ ๋งค์šฐ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

function App() {
  return (
    <Routes>
      <Route path="/" element={<Layout />}>
        <Route path="invoices" element={<Invoices />} />
        <Route path="activity" element={<Activity />} />
      </Route>
    </Routes>
  );
}

function Layout() {
  return (
    <div>
      <GlobalNav />
      <main>
        <Outlet />
      </main>
    </div>
  );
}

์ด๋ ‡๊ฒŒ ๋ ˆ์ด์•„์›ƒ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค๊ณ , react-router-dom์—์„œ Outlet ์ปดํฌ๋„ŒํŠธ๋ฅผ importํ•œ๋‹ค. ๊ณ ์ •ํ•  ์ปดํฌ๋„ŒํŠธ๋“ค์ด๋‚˜ ์Šคํƒ€์ผ๋“ค์„ ์„ค์ •ํ•ด์ฃผ๊ณ  routes์•ˆ์˜ route์— ๋ ˆ์ด์•„์›ƒ ์ปดํฌ๋„ŒํŠธ๋กœ ๊ฐ์‹ธ์ฃผ๋ฉด ๊ทธ ๋ฐ‘์˜ route๋“ค์€ Outlet ์ปดํฌ๋„ŒํŠธ ์œ„์น˜์— ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค๐ŸŒŸ

0๊ฐœ์˜ ๋Œ“๊ธ€