๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐Ÿ–ฅ dev-ruby

[React] ๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ(Router) ์„ค์ •ํ•˜๊ธฐ ๋ณธ๋ฌธ

React

[React] ๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ(Router) ์„ค์ •ํ•˜๊ธฐ

ruby_s 2021. 11. 28. 02:24
728x90
๋ฐ˜์‘ํ˜•
SMALL

์ตœ๊ทผ ๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ๊ฐ€ v5 ์—์„œ v6๋กœ ์—…๋ฐ์ดํŠธ ๋˜๋ฉด์„œ ๋ฐ”๋€ ๊ฒƒ๋“ค์„ ์ •๋ฆฌํ•ด๋ณผ๊นŒ ํ•œ๋‹ค.

๋ผ์šฐํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ

์„ค์น˜

: ๋จผ์ € ์„ค์น˜๋ฅผ ํ•ด์ค€๋‹ค. ๋’ค์— 6์€ ๋ฒ„์ „์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

npm install react-router-dom@6

 

URL ์—ฐ๊ฒฐ

๋จผ์ € ์•ฑ์„ ๋ธŒ๋ผ์šฐ์ €์˜ URL์— ์—ฐ๊ฒฐํ•œ๋‹ค. BrowserRouter์„ ์ „์ฒด ์•ฑ์— ๋ Œ๋”๋งํ•œ๋‹ค.

index.js

import { render } from "react-dom";
import { BrowserRouter } from "react-router-dom";
import App from "./App";

const rootElement = document.getElementById("root");
render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  rootElement
);

 

Routes

: ์„œ๋กœ ๋‹ค๋ฅธ URL์—์„œ ์•ฑ์„ ๋ Œ๋”๋ง ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

: v6๊ฐ€ ๋ฆด๋ฆฌ์ฆˆ๋˜๋ฉด์„œ Switch๊ฐ€ Routes๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค.

index.js

import { render } from "react-dom";
import {
  BrowserRouter,
  Routes,
  Route
} from "react-router-dom";
import App from "./App";
import Home from "./routes/home";
import About from "./routes/about";

const rootElement = document.getElementById("root");
render(
  <BrowserRouter>
    <Routes>
      <Route path="/" element={<App />} />
      <Route path="home" element={<Home />} />
      <Route path="about" element={<About />} />
    </Routes>
  </BrowserRouter>,
  rootElement
);

<Routes>์•ˆ์— ๋šซ์„ ๋ผ์šฐํ„ฐ๋ฅผ ๋‚˜์—ดํ•ด์ค€๋‹ค.

์ด์ œ "/home"์œผ๋กœ ์ ‘๊ทผํ•˜๋ฉด ๋ฅผ ์„ ์–ธํ•œ ๊ณณ์— Home์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง ๋  ๊ฒƒ์ด๋‹ค.

component๊ฐ€ ์‚ฌ๋ผ์ง€๊ณ , ๋Œ€์‹ ์— element ์‚ฌ์šฉํ•œ๋‹ค.

<Route path="/" element={<Home />} />
<Route path="/login" element={<Login />} />

 

Route์— exact๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ๋˜์–ด์žˆ์Œ

๊ธฐ์กด ์ฝ”๋“œ

<Route path="/login" exact component={UsersPage} />

v6 ์ฝ”๋“œ

<Route path="/login" element={<UsersPage />} />

 

useNavigate

: useHistory ๊ธฐ๋Šฅ์„ ์ „๋ถ€ ๋Œ€์ฒดํ•œ๋‹ค.

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

const navigate = useNavigate();

navigate('/');	// "/"๊ฒฝ๋กœ๋กœ ์ด๋™
navigate(-1);
navigate(-2);
navigate(`/user/${user._id}`);

 

Link

: ํ•ด๋‹น ๋งํฌ๋ฅผ ํด๋ฆญํ•˜๋ฉด to์— ํ• ๋‹น๋œ ์ฃผ์†Œ๋กœ ์ด๋™ํ•œ๋‹ค.

์ด๋™ํ•˜๋ฉด to ๋งํฌ๋กœ Route์—์„œ ์„ค์ •ํ•œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง๋œ๋‹ค.

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

function Home() {
  return (
    <div>
      <h1>Home</h1>
      <nav>
        <Link to="/">Home</Link> |{" "}
        <Link to="about">About</Link>
      </nav>
    </div>
  );
}

 

์„œ๋ธŒ๋ผ์šฐํŠธ 1

: path์— *์‚ฌ์šฉ

<Route path="/users/*" element={<Users />} />

์„œ๋ธŒ ๋ผ์šฐํŠธ 2

: Outlet ์‚ฌ์šฉ

App.js

<Route path="/users/*" element={<Users />} >
  <Route path="" element={<Main />} />
  <Route path="about" element={<About />} />
</Route>

Users.jsx

<Outlet />

 

Nested Routes

: ์ค‘์ฒฉ๋ผ์šฐํ„ฐ

<Routes>
	<Route path="/" element={<App />}>
   		<Route path="expenses" element={<Expenses />} />
    		<Route path="invoices" element={<Invoices />} />
	</Route>
</Routes>

"/expenses"์™€  "/invoices" ๋กœ / ๋’ค์— ์ค‘์ฒฉ๋œ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๊ทธ ์ „์— "๋ถ€๋ชจ" ๊ฒฝ๋กœ์— Outlet๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

๋ถ€๋ชจ๊ฒฝ๋กœ๋Š” "/"๊ฐ€ ๋ ๊ฑฐ๊ณ , /์—์„œ invoices๋‚˜ expenses๋ฅผ ํด๋ฆญํ•˜๋ฉด Outlet์— ์ค‘์ฒฉ์œผ๋กœ ๋ Œ๋”๋ง์ด ๋  ๊ฒƒ์ด๋‹ค.

<div>
	<Link to="/invoices">Invoices</Link> |{" "}
    	<Link to="/expenses">Expenses</Link>
    	<Outlet />
</div>

 

์ƒ๋Œ€๊ฒฝ๋กœ ์“ฐ๊ธฐ

: Linkํƒœ๊ทธ์˜ to ๊ฒฝ๋กœ ์•ž์— / ๋ฅผ ๋ถ™์ด์ง€ ์•Š์œผ๋ฉด ๋œ๋‹ค.

<Link to="" />		// ์ด๋ ‡๊ฒŒ ์ž…๋ ฅํ•˜๋ฉด ํ˜„์žฌ ํŽ˜์ด์ง€๋กœ ์ด๋™
<Link to="about" />	// ์ด๋ ‡๊ฒŒ ์ž…๋ ฅํ•˜๋ฉด ํ˜„์žฌ url์— /about์„ ๋ถ™์ธ ๊ณณ์œผ๋กœ ์ด๋™ -> ๋‹จ, about์•ž์— /about์„ ๋ถ™์ด๊ฒŒ๋˜๋ฉด ์ง„์งœ /about์œผ๋กœ ์ด๋™๋œ๋‹ค.

<Route path="" />
<Route path="about" />

 

NavLink ์— ์Šคํƒ€์ผ ์ ์šฉํ•˜๊ธฐ

: isActive ๋กœ ์‚ผํ•ญ์—ฐ์‚ฐ์ž ์ ์šฉํ•˜๊ธฐ

<NavLink to="/users" style={({ isActive }) => ({ color: isActive ? 'blue' : 'red' })} >
  Users
</NavLink>

<NavLink to="/users" className={({ isActive }) => "nav-link" + (isActive ? "activated" : "") } >
    Users
</NavLink>

 

728x90
๋ฐ˜์‘ํ˜•
LIST