React Router

Doozuuยท2023๋…„ 2์›” 8์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
10/23
post-custom-banner

๐Ÿ“Œ React Router

ํŽ˜์ด์ง€ ์ด๋™์„ ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

์ •ํ™•ํžˆ๋Š” ํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฐ™์€ ํŽ˜์ด์ง€์—์„œ Router๋ฅผ ์ด์šฉํ•ด ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ ๋ฟ์ž„.

์„ค์น˜

cd ํ”„๋กœ์ ํŠธํด๋”
npm install react-router-dom
// ํ˜น์€ yarn add react-router-dom

๋ฒ„์ „ ์—…๊ทธ๋ ˆ์ด๋“œ ๋˜๋ฉด์„œ ๋ฐ”๋€Œ๋Š” ๋ถ€๋ถ„๋“ค์€ React Router ์‚ฌ์ดํŠธ์—์„œ ํ™•์ธํ•˜๊ธฐ.
https://reactrouter.com/en/main

์‚ฌ์šฉ๋ฐฉ๋ฒ•

import {BrowerRouter} from 'react-router-dom'

Outlet

๋ถ€๋ชจ Route์—์„œ ์ž์‹ Route๋ฅผ ๋ณด์ด๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ณ , ๋‚˜์˜ค๋Š” ์œ„์น˜๋„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

import

import { Outlet } from 'react-router-dom'

์‚ฌ์šฉํ•˜๊ธฐ
<Outlet />

const App = () => {
  return (
    <Routes>
      <Route path="/" element={<Navigation />}>
        <Route index element={<Home />} />
      </Route>
    </Routes>
  );
};
const Navigation = () => {
  return (
    <div>
      <div>
        <h1>navigation bar</h1>
      </div>
      <Outlet />
    </div>
  );
};

<Link to='์ด๋™๊ฒฝ๋กœ' element={<๋ณด์—ฌ์ค„ ์ปดํฌ๋„ŒํŠธ/>}/>

profile
๋ชจ๋“ ๊ฒŒ ์ƒˆ๋กญ๊ณ  ์žฌ๋ฐŒ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ์ƒˆ์‹น
post-custom-banner

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