3.Router

<Router>๋Š” ๋ชจ๋“  ๋ผ์šฐํ„ฐ ์ปดํฌ๋„ŒํŠธ(์˜ˆ: <BrowserRouter> ๋ฐ <StaticRouter>)๊ฐ€ ๊ณต์œ ํ•˜๋Š” ๋กœ์šฐ๋ ˆ๋ฒจ ์ธํ„ฐํŽ˜์ด์Šค๋‹ค. React์˜ ๊ด€์ ์—์„œ <Router>๋Š” ์•ฑ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์— ๋ผ์šฐํŒ… ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ปจํ…์ŠคํŠธ ํ”„๋กœ๋ฐ”์ด๋”(Context Provider)๋‹ค.

์•„๋งˆ <Router>๋ฅผ ์ˆ˜๋™์œผ๋กœ ๋ Œ๋”๋งํ•  ํ•„์š”๋Š” ์—†์„ ๊ฒƒ์ด๋‹ค. ๋Œ€์‹  ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ์ƒ์œ„ ๋ ˆ๋ฒจ ๋ผ์šฐํ„ฐ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ํŠน์ • ์•ฑ์—๋Š” ๋ผ์šฐํ„ฐ๊ฐ€ ํ•˜๋‚˜๋งŒ ํ•„์š”ํ•˜๋‹ค.

<Router basename> ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ฑ์˜ ๋ชจ๋“  ๊ฒฝ๋กœ์™€ ๋งํฌ๊ฐ€ ๋ชจ๋‘ ๊ณต์œ ํ•˜๋Š” URL ๊ฒฝ๋กœ๋ช…์˜ "basename" ๋ถ€๋ถ„์— ์ƒ๋Œ€์ ์ธ ๊ฒฝ๋กœ์™€ ๋งํฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” React ๋ผ์šฐํ„ฐ๋กœ ๋” ํฐ ์•ฑ์˜ ์ผ๋ถ€๋งŒ ๋ Œ๋”๋งํ•˜๊ฑฐ๋‚˜ ์•ฑ์— ์—ฌ๋Ÿฌ ์ง„์ž…์ ์ด ์žˆ๋Š” ๊ฒฝ์šฐ์— ์œ ์šฉํ•˜๋‹ค. "basename"์€ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋‹ค.

ReactRouter - RouterProvider

RouterProvider๋Š” React Router v6์—์„œ ์ œ๊ณตํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ด ์ปดํฌ๋„ŒํŠธ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ ๋ผ์šฐํ„ฐ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด ์•ฑ์„ ๋ Œ๋”๋งํ•˜๊ณ  ๋‚˜๋จธ์ง€ ๋ฐ์ดํ„ฐ API๋ฅผ ํ™œ์„ฑํ™”ํ•œ๋‹ค.

RouterProvider๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด importํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

import { createBrowserRouter, RouterProvider } from "react-router-dom";

const router = createBrowserRouter([
  {
    path: "/",
    element: <Root />,
    children: [
      { path: "dashboard", element: <Dashboard /> },
      { path: "about", element: <About /> },
    ],
  },
]);

ReactDOM.createRoot(document.getElementById("root")).render(
  <RouterProvider router={router} fallbackElement={<BigSpinner />} />
);

Last updated