๋ฐ˜์‘ํ˜•

๐Ÿ”ฐ ๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ v6 ๋ฒ„์ „์—์„œ ์ค‘์ฒฉ ๋ผ์šฐํŒ…์„ ์ด์šฉํ•˜๋ฉด ๋ถ€๋ชจ ๋ ˆ์ด์•„์›ƒ์€ ๊ทธ๋Œ€๋กœ ๋‘๊ณ  ์ž์‹ ๋ ˆ์ด์•„์›ƒ๋งŒ ์‰ฝ๊ฒŒ ๊ต์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โœ…  ์ค‘์ฒฉ ๋ผ์šฐํŒ…

v6์—์„œ ์ค‘์ฒฉ ๋ผ์šฐํŒ…์ด ์ปดํฌ๋„ŒํŠธ์˜ children๊ณผ ๊ฐ™์€ ๊ฐœ๋…์œผ๋กœ ์ค‘์ฒฉ์ด ๊ฐ€๋Šฅํ•ด์กŒ๊ณ , ๋” ์ง๊ด€์ ์œผ๋กœ ๋ณ€ํ–ˆ๋‹ค.

๋งŒ์•ฝ, member ์™€ member/:memberId ํŽ˜์ด์ง€๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค๋ฉด, ์ด์ „์—๋Š” ๊ฐ๊ฐ ํ•œ์ค„์”ฉ ์ฐจ์ง€ํ•˜์˜€๋Š”๋ฐ,

v6์—์„œ๋Š” member ๋ผ์šฐํŠธ ํ•˜์œ„์— :memberId ๊ฐ€ ํฌํ•จ๋œ Route๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์ค‘์ฒฉ ๋ผ์šฐํŒ…์ด ๋œ๋‹ค.

ํ•˜์œ„์— ์žˆ์œผ๋ฉด ์ž๋™์œผ๋กœ / ๋กœ ๊ตฌ๋ถ„๋˜๊ธฐ ๋–„๋ฌธ์— ์ถ”๊ฐ€์ ์ธ / ๋ฅผ path์— ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†์–ด์กŒ๋‹ค.

 

//v5
<Switch>
  <Route path="/member" />
  <Route path="/member/:memberId" />
</Switch>

//v6
<Routes>
  <Route path="/member">
    <Route path=":memberId" /> // /member/:memberId
  </Route>
</Routes>

 

โœ…  ๋ ˆ์ด์•„์›ƒ

์ค‘์ฒฉ ๋ผ์šฐํŒ…์„ ์œ„์ฒ˜๋Ÿผ ๊ตฌ์„ฑํ•˜๊ณ  ๋‚˜๋ฉด Outlet์„ ํ†ตํ•ด์„œ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ ˆ์ด์•„์›ƒํ™” ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

์‹ค์ œ๋กœ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ด๋ณด์ž.

 

 

[index.js]

"/"๋ผ๋Š” path๋ฅผ ๊ฐ€์ง€๋ฉด App1 ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‹คํ–‰๋˜๊ณ  App1์˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋Š” First, Second, Third๊ฐ€ ์žˆ๋‹ค.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ "/app2" ๋ผ๋Š” path๋ฅผ ๊ฐ€์ง€๋ฉด App2 ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‹คํ–‰๋˜๊ณ  App2์˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋Š” First, Second, Third๊ฐ€ ์žˆ๋‹ค.

import React from "react";
import ReactDOM from "react-dom/client";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import App1 from "./App1";
import App2 from "./App2";
import First from "./First";
import NotFound from "./NotFound";
import Second from "./Second";
import Third from "./Third";

const router = createBrowserRouter([
  {
    path: "/",
    element: <App1 />,
    errorElement: <NotFound />,
    children: [
      { index: true, element: <First /> },
      { path: "second", element: <Second /> },
      { path: "third", element: <Third /> },
    ],
  },
  {
    path: "/app2",
    element: <App2 />,
    errorElement: <NotFound />,
    children: [
      { index: true, element: <First /> },
      { path: "second", element: <Second /> },
      { path: "third", element: <Third /> },
    ],
  },
]);

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <RouterProvider router={router} />
  </React.StrictMode>
);

 

 

[App1.js]

App1์—์„œ Outlet์„ ํ†ตํ•ด ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ ˆ์ด์•„์›ƒํ™” ํ•˜๊ณ  ์žˆ๋‹ค.

import { Outlet } from "react-router-dom";

function App1() {
  return (
    <div>
      <h1>This is App1 Header</h1>
      <Outlet />
      <h2>This is App1 Footer</h2>
    </div>
  );
}

export default App1;

 

[App2.js]

App2 ๋˜ํ•œ Outlet์„ ํ†ตํ•ด ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ ˆ์ด์•„์›ƒํ™” ํ•˜๊ณ  ์žˆ๋‹ค.

import { Outlet } from "react-router-dom";

function App2() {
  return (
    <div>
      <h1>This is App2 Header</h1>
      <Outlet />
      <h2>This is App2 Footer</h2>
    </div>
  );
}

export default App2;

 

[First, Second, Third.js]

function First() {
  return <div className="First">Hello world First</div>;
}

export default First;
function Second() {
  return <div className="Second">Hello world Second</div>;
}

export default Second;
function Third() {
  return <div className="Third">Hello world Third</div>;
}

export default Third;

 

 

โœ…  ๊ฒฐ๊ณผํ™”๋ฉด

์ค‘์ฒฉ ๋ผ์šฐํŒ…์„ ์œ„์ฒ˜๋Ÿผ ๊ตฌ์„ฑํ•˜๊ณ  ๋‚˜๋ฉด Outlet์„ ํ†ตํ•ด์„œ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ ˆ์ด์•„์›ƒํ™” ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

์‹ค์ œ๋กœ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ด๋ณด์ž.

 

์•„๋ž˜๋Š” App1๊ณผ App2์˜ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ค‘์ฒฉ ๋ผ์šฐํŒ…์„ ์ด๋ฃจ๊ณ  ์žˆ์œผ๋ฉฐ ์„œ๋กœ Outlet ์„ ํ†ตํ•ด ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ‘œํ˜„ํ•˜๊ณ ์žˆ๋‹ค.

http://localhost:3000/

 

http://localhost:3000/second
http://localhost:3000/third

 


 

http://localhost:3000/app2

 

http://localhost:3000/app2/second
http://localhost:3000/app2/third

๋ฐ˜์‘ํ˜•