Search

Router

Router

React μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ URL을 κ΄€λ¦¬ν•˜λŠ” 라이브러리

μ •μ˜

React λΌμš°ν„°λŠ” React μ• ν”Œλ¦¬μΌ€μ΄μ…˜ λ‚΄μ—μ„œ νŽ˜μ΄μ§€ κ°„μ˜ λ„€λΉ„κ²Œμ΄μ…˜μ„ κ΄€λ¦¬ν•˜λŠ” λΌμ΄λΈŒλŸ¬λ¦¬μž…λ‹ˆλ‹€. 주둜 μ‹±κΈ€ νŽ˜μ΄μ§€ μ• ν”Œλ¦¬μΌ€μ΄μ…˜(Single Page Application, SPA)μ—μ„œ μ‚¬μš©λ˜λ©°, URL을 기반으둜 λ‹€μ–‘ν•œ μ»΄ν¬λ„ŒνŠΈλ₯Ό λ Œλ”λ§ν•˜μ—¬ μ‚¬μš©μž κ²½ν—˜μ„ ν–₯μƒμ‹œν‚΅λ‹ˆλ‹€.
Router λ₯Ό μ‚¬μš©ν•˜λ©΄ URL을 기반으둜 μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ λ Œλ”λ§μ„ μ œμ–΄ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μ‚¬μš©λͺ©μ 

React λΌμš°ν„°λ₯Ό μ‚¬μš©ν•˜λ©΄ λ‹€μŒκ³Ό 같은 μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
β€’
μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ 각 νŽ˜μ΄μ§€μ— λŒ€ν•œ URL을 μ •μ˜ν•©λ‹ˆλ‹€.
β€’
μ‚¬μš©μžκ°€ URL을 λ³€κ²½ν•  λ•Œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ λ Œλ”λ§μ„ μ—…λ°μ΄νŠΈν•©λ‹ˆλ‹€.
β€’
μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ 각 νŽ˜μ΄μ§€μ— λŒ€ν•œ μ•‘μ„ΈμŠ€λ₯Ό μ œμ–΄ν•©λ‹ˆλ‹€.

일반적인 νŽ˜μ΄μ§€ 이동과 λΌμš°νŒ…μ˜ 차이

νŠΉμ§•
일반적인 νŽ˜μ΄μ§€ 이동
λΌμš°νŒ…
HTTP μš”μ²­
μƒˆ νŽ˜μ΄μ§€μ˜ HTML, CSS, JavaScript νŒŒμΌμ„ λ‹€μš΄λ‘œλ“œ
κΈ°μ‘΄ νŽ˜μ΄μ§€μ˜ DOM을 μ—…λ°μ΄νŠΈ
λΈŒλΌμš°μ €μ˜ λ Œλ”λ§
λΈŒλΌμš°μ €μ˜ DOM을 μ™„μ „νžˆ μƒˆλ‘œ λ Œλ”λ§
κΈ°μ‘΄ νŽ˜μ΄μ§€μ˜ DOM을 μ—…λ°μ΄νŠΈ
μž₯점
κΈ°μ‘΄ μ›Ή 개발 λ°©μ‹κ³Όμ˜ ν˜Έν™˜μ„±
λΈŒλΌμš°μ €μ˜ λ¦¬μ†ŒμŠ€λ₯Ό μ ˆμ•½ν•˜κ³  λ‘œλ”© 속도λ₯Ό κ°œμ„ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
단점
μƒˆλ‘œμš΄ νŽ˜μ΄μ§€λ₯Ό μ™„μ „νžˆ μƒˆλ‘œ λ‘œλ“œν•΄μ•Ό ν•˜λŠ” 경우, λ¦¬μ†ŒμŠ€λ₯Ό 많이 μ†Œλͺ¨ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
λΌμš°νŒ…μ„ κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ 라이브러리 λ˜λŠ” ν”„λ ˆμž„μ›Œν¬κ°€ ν•„μš”ν•©λ‹ˆλ‹€.

일반적인 νŽ˜μ΄μ§€ 이동

일반적인 νŽ˜μ΄μ§€ 이동은 λΈŒλΌμš°μ €μ—μ„œ HTTP μš”μ²­μ„ 톡해 μƒˆλ‘œμš΄ νŽ˜μ΄μ§€λ₯Ό λ‘œλ“œν•˜λŠ” 것을 μ˜λ―Έν•©λ‹ˆλ‹€. 이 경우 λΈŒλΌμš°μ €λŠ” μƒˆ νŽ˜μ΄μ§€μ˜ HTML, CSS, JavaScript νŒŒμΌμ„ λ‹€μš΄λ‘œλ“œν•˜κ³ , λΈŒλΌμš°μ €μ˜ DOM에 μƒˆ νŽ˜μ΄μ§€λ₯Ό λ Œλ”λ§ν•©λ‹ˆλ‹€.

λΌμš°νŒ…

λΌμš°νŒ…μ€ νŽ˜μ΄μ§€ 이동을 보닀 효율적으둜 μ²˜λ¦¬ν•˜κΈ° μœ„ν•œ λ°©λ²•μž…λ‹ˆλ‹€. λΌμš°νŒ…μ„ μ‚¬μš©ν•˜λ©΄ λΈŒλΌμš°μ €κ°€ μƒˆλ‘œμš΄ νŽ˜μ΄μ§€λ₯Ό μ™„μ „νžˆ λ‘œλ“œν•˜λŠ” λŒ€μ‹ , κΈ°μ‘΄ νŽ˜μ΄μ§€μ˜ DOM을 μ—…λ°μ΄νŠΈν•˜μ—¬ μƒˆλ‘œμš΄ νŽ˜μ΄μ§€λ₯Ό ν‘œμ‹œν•©λ‹ˆλ‹€.

μ„€μΉ˜

React λΌμš°ν„°λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” 라이브러리λ₯Ό μ„€μΉ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€.
npm install react-router-dom
JavaScript
볡사

문법

μ£Όμš” ν‚€μ›Œλ“œ

v5 (react-router-dom)
ν‚€μ›Œλ“œ
μ„€λͺ…
BrowserRouter
λΈŒλΌμš°μ €μ—μ„œ λΌμš°νŒ…μ„ ν™œμ„±ν™”ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈ
Route
URL νŒ¨ν„΄κ³Ό λ Œλ”λ§ν•  μ»΄ν¬λ„ŒνŠΈλ₯Ό μ •μ˜ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈ
Switch
μ—¬λŸ¬Β RouteΒ μ»΄ν¬λ„ŒνŠΈλ₯Ό κ·Έλ£Ήν™”ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈ
Link
URL을 λ§ν¬ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈ
to
LinkΒ μ»΄ν¬λ„ŒνŠΈμ˜ μ†μ„±μœΌλ‘œ, 이동할 경둜λ₯Ό μ§€μ •ν•©λ‹ˆλ‹€.
exact
RouteΒ μ»΄ν¬λ„ŒνŠΈμ˜ μ†μ„±μœΌλ‘œ, κ²½λ‘œκ°€ μ •ν™•νžˆ μΌμΉ˜ν•΄μ•Όλ§Œ λ Œλ”λ§λ©λ‹ˆλ‹€.
param, params
RouteΒ μ»΄ν¬λ„ŒνŠΈμ˜ μ†μ„±μœΌλ‘œ, URL에 λ§€κ°œλ³€μˆ˜λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.
redirect
RouteΒ μ»΄ν¬λ„ŒνŠΈμ˜ μ†μ„±μœΌλ‘œ, λ‹€λ₯Έ URL둜 λ¦¬λ””λ ‰μ…˜ν•©λ‹ˆλ‹€.
component
RouteΒ μ»΄ν¬λ„ŒνŠΈμ˜ μ†μ„±μœΌλ‘œ, λ Œλ”λ§ν•  μ»΄ν¬λ„ŒνŠΈλ₯Ό μ§€μ •ν•©λ‹ˆλ‹€.

Hooks

ν‚€μ›Œλ“œ
μ„€λͺ…
withRouter
λΌμš°νŒ… 정보λ₯Ό μ‚¬μš©ν•  수 μžˆλŠ” Hook
useHistory
λΈŒλΌμš°μ €μ˜ URL을 직접 μ‘°μž‘ν•  수 μžˆλŠ” Hook
useLocation
ν˜„μž¬ URL을 κ°€μ Έμ˜¬ 수 μžˆλŠ” Hook
useParams
URL에 μ§€μ •λœ λ§€κ°œλ³€μˆ˜λ₯Ό κ°€μ Έμ˜¬ 수 μžˆλŠ” Hook
v6 (react-router-dom)

ν‚€μ›Œλ“œ

ν‚€μ›Œλ“œ
μ„€λͺ…
BrowserRouter
λΈŒλΌμš°μ €μ˜ History APIλ₯Ό μ‚¬μš©ν•˜μ—¬ λΌμš°νŒ…μ„ κ΄€λ¦¬ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈμž…λ‹ˆλ‹€.
HashRouter
λΈŒλΌμš°μ €μ˜ URL ν•΄μ‹œλ₯Ό μ‚¬μš©ν•˜μ—¬ λΌμš°νŒ…μ„ κ΄€λ¦¬ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈμž…λ‹ˆλ‹€.
Routes
λΌμš°νŒ… κ·œμΉ™μ„ μ •μ˜ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈμž…λ‹ˆλ‹€.
Route
νŠΉμ • κ²½λ‘œμ— ν•΄λ‹Ήν•˜λŠ” μ»΄ν¬λ„ŒνŠΈλ₯Ό λ Œλ”λ§ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈμž…λ‹ˆλ‹€.
Link
νŠΉμ • 경둜둜 μ΄λ™ν•˜λŠ” 링크λ₯Ό λ Œλ”λ§ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈμž…λ‹ˆλ‹€.
useHistory
History APIλ₯Ό μ‚¬μš©ν•  수 μžˆλŠ” ν›…μž…λ‹ˆλ‹€.
useNavigate
History APIλ₯Ό μ‚¬μš©ν•˜μ—¬ νŠΉμ • 경둜둜 μ΄λ™ν•˜λŠ” ν›…μž…λ‹ˆλ‹€.
useLocation
ν˜„μž¬ μœ„μΉ˜ 정보λ₯Ό κ°€μ Έμ˜¬ 수 μžˆλŠ” ν›…μž…λ‹ˆλ‹€.
useParams
경둜 νŒŒλΌλ―Έν„°λ₯Ό κ°€μ Έμ˜¬ 수 μžˆλŠ” ν›…μž…λ‹ˆλ‹€.
useQuery
쿼리 μŠ€νŠΈλ§μ„ κ°€μ Έμ˜¬ 수 μžˆλŠ” ν›…μž…λ‹ˆλ‹€.
β€’
v5
import React from "react"; import { BrowserRouter, Route, Switch } from "react-router-dom"; function App() { return ( <BrowserRouter> <Switch> <Route path="/" exact component={Home} /> <Route path="/about" component={About} /> </Switch> </BrowserRouter> ); } const Home = () => { return ( <div> <h1>ν™ˆνŽ˜μ΄μ§€</h1> <Link to="/about">μ†Œκ°œ</Link> </div> ); }; const About = () => { return ( <div> <h1>μ†Œκ°œ</h1> <Link to="/">ν™ˆνŽ˜μ΄μ§€</Link> </div> ); };
JavaScript
볡사
μœ„ μ½”λ“œμ—μ„œλŠ” BrowserRouter μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜μ—¬ λΈŒλΌμš°μ €μ—μ„œ λΌμš°νŒ…μ„ ν™œμ„±ν™”ν•©λ‹ˆλ‹€. Switch μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜μ—¬ μ—¬λŸ¬ Route μ»΄ν¬λ„ŒνŠΈλ₯Ό κ·Έλ£Ήν™”ν•©λ‹ˆλ‹€. Route μ»΄ν¬λ„ŒνŠΈλŠ” URL νŒ¨ν„΄κ³Ό λ Œλ”λ§ν•  μ»΄ν¬λ„ŒνŠΈλ₯Ό μ •μ˜ν•©λ‹ˆλ‹€.
β€’
v6
import logo from './logo.svg'; import './App.css'; import { BrowserRouter, Link, Route, Routes } from 'react-router-dom' function App() { return ( <BrowserRouter> <Routes> <Route path="/" element={ <Home /> } /> <Route path="/about" element={ <About /> } /> </Routes> </BrowserRouter> ); } // πŸ”— / function Home() { return ( <> <h1>Home</h1> <Link to="/about">About</Link> </> ) } // πŸ”— /about function About() { return ( <> <h1>About</h1> <Link to="/">Home</Link> </> ) } export default App;
JavaScript
볡사

BrowserRouter

λΈŒλΌμš°μ €μ—μ„œ λΌμš°νŒ…μ„ ν™œμ„±ν™”ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈ
BrowserRouterλŠ” λΈŒλΌμš°μ €μ—μ„œ λΌμš°νŒ…μ„ ν™œμ„±ν™”ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈμž…λ‹ˆλ‹€. BrowserRouterλ₯Ό μ‚¬μš©ν•˜λ©΄ React μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ URL을 λ³€κ²½ν•  λ•Œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ λ Œλ”λ§μ„ μ—…λ°μ΄νŠΈν•  수 μžˆμŠ΅λ‹ˆλ‹€.
BrowserRouterλŠ” λ‹€μŒκ³Ό 같은 속성을 μ œκ³΅ν•©λ‹ˆλ‹€.
β€’
basename:Β URL의 κΈ°λ³Έ 경둜λ₯Ό μ§€μ •ν•©λ‹ˆλ‹€.
β€’
history:Β λΈŒλΌμš°μ €μ˜ URL을 μ‘°μž‘ν•˜λŠ” 데 μ‚¬μš©ν•  수 μžˆλŠ” History μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€.
import React from "react"; import { BrowserRouter, Route, Switch } from "react-router-dom"; function App() { return ( <BrowserRouter basename="/my-app"> <Switch> <Route path="/" exact component={Home} /> <Route path="/about" component={About} /> </Switch> </BrowserRouter> ); } const Home = () => { return <h1>ν™ˆνŽ˜μ΄μ§€</h1>; }; const About = () => { return <h1>μ†Œκ°œ</h1>; };
JavaScript
볡사
http://localhost:3000/my-app/
Plain Text
볡사

Route

URL νŒ¨ν„΄κ³Ό λ Œλ”λ§ν•  μ»΄ν¬λ„ŒνŠΈλ₯Ό μ •μ˜ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈ
RouteλŠ” URL νŒ¨ν„΄κ³Ό λ Œλ”λ§ν•  μ»΄ν¬λ„ŒνŠΈλ₯Ό μ •μ˜ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈμž…λ‹ˆλ‹€. Route μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜μ—¬ React μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ λΌμš°νŒ…μ„ κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
Route μ»΄ν¬λ„ŒνŠΈλŠ” λ‹€μŒκ³Ό 같은 속성을 μ œκ³΅ν•©λ‹ˆλ‹€.
β€’
path:Β λΌμš°νŒ…ν•  URL νŒ¨ν„΄μž…λ‹ˆλ‹€.
β€’
exact:Β κ²½λ‘œκ°€ μ •ν™•νžˆ μΌμΉ˜ν•΄μ•Όλ§Œ λ Œλ”λ§λ©λ‹ˆλ‹€.
β€’
params:Β URL에 λ§€κ°œλ³€μˆ˜λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.
β€’
redirect:Β λ‹€λ₯Έ URL둜 λ¦¬λ””λ ‰μ…˜ν•©λ‹ˆλ‹€.
β€’
component:Β λ Œλ”λ§ν•  μ»΄ν¬λ„ŒνŠΈλ₯Ό μ§€μ •ν•©λ‹ˆλ‹€.

κΈ°λ³Έ μ˜ˆμ‹œ

<Route path="/" exact component={Home} /> <Route path="/about" component={About} />
JavaScript
볡사

params 속성 μ‚¬μš© μ˜ˆμ‹œ

β€’
β€’ params 속성을 μ‚¬μš©ν•˜μ—¬Β idΒ λ§€κ°œλ³€μˆ˜μ˜ 값을 μ§€μ •ν•©λ‹ˆλ‹€.
import React, { useState } from "react"; import { BrowserRouter, Route, Switch } from "react-router-dom"; function App() { return ( <BrowserRouter> <Switch> <Route path="/" exact component={Home} /> <Route path="/boards/:id" component={Board} params={{ id: 123 }} /> </Switch> </BrowserRouter> ); } const Home = () => { return <h1>ν™ˆνŽ˜μ΄μ§€</h1>; }; const Board = () => { const { id } = useParams(); return ( <div> <h2>κ²Œμ‹œλ¬Ό 쑰회</h2> <p>κ²Œμ‹œλ¬Ό ID: {id}</p> </div> ); };
JavaScript
볡사

redirect 속성 μ‚¬μš© μ˜ˆμ‹œ

β€’ /adminΒ κ²½λ‘œλŠ” λ‘œκ·ΈμΈν•˜μ§€ μ•Šμ€ μ‚¬μš©μžμ—κ²Œ 둜그인 νŽ˜μ΄μ§€λ‘œ λ¦¬λ””λ ‰μ…˜ν•©λ‹ˆλ‹€.
import React, { useState } from "react"; import { BrowserRouter, Route, Switch } from "react-router-dom"; function App() { const [isLoggedIn, setIsLoggedIn] = useState(false); return ( <BrowserRouter> <Switch> <Route path="/" exact component={Home} /> <Route path="/admin" render={() => { if (isLoggedIn) { return <Admin />; } else { return <Redirect to="/login" />; } }} /> <Route path="/boards/:id" component={Board} /> <Route path="/boards/:id/delete" redirect="/boards" /> </Switch> </BrowserRouter> ); } const Home = () => { return <h1>ν™ˆνŽ˜μ΄μ§€</h1>; }; const Board = () => { const { id } = useParams(); return ( <div> <h2>κ²Œμ‹œλ¬Ό 쑰회</h2> <p>κ²Œμ‹œλ¬Ό ID: {id}</p> </div> ); }; const Admin = () => { return <h1>κ΄€λ¦¬μž νŽ˜μ΄μ§€</h1>; };
JavaScript
볡사

μ •κ·œμ‹ URL νŒ¨ν„΄ μž‘μ„±

path μ†μ„±μ˜ κ²½λ‘œμ— λ§€κ°œλ³€μˆ˜κ°€ ν¬ν•¨λ˜λŠ” 경우, μ •κ·œμ‹μœΌλ‘œ νŒ¨ν„΄ 맀핑을 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
<Route path="/경둜/:νŒŒλΌλ―Έν„°[μ •κ·œμ‹]" component={μ»΄ν¬λ„ŒνŠΈ} />
JavaScript
볡사
μ½”λ“œλŠ” /경둜 κ²½λ‘œμ— :νŒŒλΌλ―Έν„° λ§€κ°œλ³€μˆ˜κ°€ ν¬ν•¨λœ κ²½μš°μ—λ§Œ μ»΄ν¬λ„ŒνŠΈλ₯Ό λ Œλ”λ§ν•©λ‹ˆλ‹€.
μ—¬κΈ°μ„œ :νŒŒλΌλ―Έν„°λŠ” μ •κ·œμ‹μ΄λ©°, λ‹€μŒκ³Ό 같은 의미λ₯Ό κ°–μŠ΅λ‹ˆλ‹€.
β€’
:λŠ” λ§€κ°œλ³€μˆ˜λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.
β€’
νŒŒλΌλ―Έν„°λŠ” λ§€κ°œλ³€μˆ˜μ˜ μ΄λ¦„μž…λ‹ˆλ‹€.
β€’
[μ •κ·œμ‹]은 λ§€κ°œλ³€μˆ˜μ˜ 값에 λŒ€ν•œ μ •κ·œμ‹μž…λ‹ˆλ‹€.
μ΄λ ‡κ²Œ μ „λ‹¬λœ :νŒŒλΌλ―Έν„° λŠ” ν•΄λ‹Ή μ»΄ν¬λ„ŒνŠΈμ—μ„œ useParams() ν›…μœΌλ‘œ κ°€μ Έμ˜¬ 수 μžˆμŠ΅λ‹ˆλ‹€.
const { id } = useParams();
JavaScript
볡사

μ˜ˆμ‹œ

<Route path="/boards/:id" component={Board} />
JavaScript
볡사
/boards κ²½λ‘œμ— λ§€κ°œλ³€μˆ˜κ°€ ν¬ν•¨λœ κ²½μš°μ—λ§Œ Board μ»΄ν¬λ„ŒνŠΈλ₯Ό λ Œλ”λ§ν•©λ‹ˆλ‹€.
μ—¬κΈ°μ„œ :idλŠ” μ •κ·œμ‹μ΄λ©°, λ‹€μŒκ³Ό 같은 의미λ₯Ό κ°–μŠ΅λ‹ˆλ‹€.
β€’
:λŠ” λ§€κ°œλ³€μˆ˜λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.
β€’
idλŠ” λ§€κ°œλ³€μˆ˜μ˜ μ΄λ¦„μž…λ‹ˆλ‹€.
/boards/:id κ²½λ‘œλŠ” λ‹€μŒκ³Ό 같은 URLκ³Ό μΌμΉ˜ν•©λ‹ˆλ‹€.
β€’
/boards/123
β€’
/boards/abc
β€’
/boards/hello
:id μ •κ·œμ‹μ€ λ‹€μŒκ³Ό 같이 λ‹€λ₯Έ κ°’μœΌλ‘œ λ³€κ²½ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.
β€’
:id([0-9]+)λŠ” 숫자만 ν¬ν•¨λœ λ§€κ°œλ³€μˆ˜λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.
β€’
:id([a-z]+)λŠ” 문자만 ν¬ν•¨λœ λ§€κ°œλ³€μˆ˜λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.
β€’
:id([a-z0-9]+)λŠ” λ¬Έμžμ™€ 숫자λ₯Ό λͺ¨λ‘ 포함할 수 μžˆλŠ” λ§€κ°œλ³€μˆ˜λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.
<Route path="/boards/:id([0-9]+)" component={Board} />
JavaScript
볡사
μœ„ μ½”λ“œλŠ” /boards κ²½λ‘œμ— 숫자만 ν¬ν•¨λœ λ§€κ°œλ³€μˆ˜κ°€ ν¬ν•¨λœ κ²½μš°μ—λ§Œ Board μ»΄ν¬λ„ŒνŠΈλ₯Ό λ Œλ”λ§ν•©λ‹ˆλ‹€.
import React, { useState } from "react"; import { BrowserRouter, Route, Switch } from "react-router-dom"; function App() { return ( <BrowserRouter> <Switch> <Route path="/" exact component={Home} /> <Route path="/users/:id)" component={User} /> <Route path="/boards/:id" component={Board} /> </Switch> </BrowserRouter> ); } const Home = () => { return <h1>ν™ˆνŽ˜μ΄μ§€</h1>; }; const User = () => { const { id } = useParams(); return ( <h1>μ‚¬μš©μž ID: {id}</h1> ); }; const Board = () => { const { id } = useParams(); return ( <h1>κ²Œμ‹œλ¬Ό ID: {id}</h1> ); };
JavaScript
볡사

Routes (v.6)

μžμ‹ Route μ»΄ν¬λ„ŒνŠΈ 쀑 ν•˜λ‚˜λ§Œ λ Œλ”λ§ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈ
Routes μ»΄ν¬λ„ŒνŠΈμ˜ μžμ‹ Route μ»΄ν¬λ„ŒνŠΈ 쀑 URLκ³Ό μΌμΉ˜ν•˜λŠ” Route μ»΄ν¬λ„ŒνŠΈλ§Œ λ Œλ”λ§λ©λ‹ˆλ‹€.
<Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> </Routes>
JavaScript
볡사

Switch (v.5)

μžμ‹ Route μ»΄ν¬λ„ŒνŠΈ 쀑 ν•˜λ‚˜λ§Œ λ Œλ”λ§ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈ
Switch μ»΄ν¬λ„ŒνŠΈμ˜ μžμ‹ Route μ»΄ν¬λ„ŒνŠΈ 쀑 URLκ³Ό μΌμΉ˜ν•˜λŠ” Route μ»΄ν¬λ„ŒνŠΈλ§Œ λ Œλ”λ§λ©λ‹ˆλ‹€.
<Switch> <Route path="/" exact component={Home} /> <Route path="/about" component={About} /> </Switch>
JavaScript
볡사

μ—­ν• 

Switchλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  Route μ—¬λŸ¬ 개λ₯Ό μ‚¬μš©ν•˜μ—¬ λΌμš°νŒ…μ„ κ΅¬ν˜„ν•˜λŠ” 경우, URL이 μΌμΉ˜ν•˜λŠ” λͺ¨λ“  Route μ»΄ν¬λ„ŒνŠΈκ°€ λ Œλ”λ§λ©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, λ‹€μŒκ³Ό 같은 μ½”λ“œλŠ” / κ²½λ‘œμ™€ /about κ²½λ‘œμ— ν•΄λ‹Ήν•˜λŠ” Route μ»΄ν¬λ„ŒνŠΈλ₯Ό λͺ¨λ‘ λ Œλ”λ§ν•©λ‹ˆλ‹€.
β€’
<Switch> μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•œ 경우
import React, { useState } from "react"; import { BrowserRouter, Route, Switch } from "react-router-dom"; function App() { return ( <BrowserRouter> <Switch> <Route path="/" exact component={Home} /> <Route path="/about" component={About} /> </Switch> </BrowserRouter> ); } const Home = () => { return <h1>ν™ˆνŽ˜μ΄μ§€</h1>; }; const About = () => { return <h1>μ†Œκ°œ</h1>; };
JavaScript
볡사
β€’
URL : /
ν™ˆνŽ˜μ΄μ§€
Plain Text
볡사
β€’
URL : /about
μ†Œκ°œ
Plain Text
볡사
β€’
<Switch> μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šμ€ 경우
import React, { useState } from "react"; import { BrowserRouter, Route } from "react-router-dom"; function App() { return ( <BrowserRouter> <Route path="/" component={Home} /> <Route path="/about" component={About} /> </BrowserRouter> ); } const Home = () => { return <h1>ν™ˆνŽ˜μ΄μ§€</h1>; }; const About = () => { return <h1>μ†Œκ°œ</h1>; };
JavaScript
볡사
β€’
URL : /
ν™ˆνŽ˜μ΄μ§€ μ†Œκ°œ
Plain Text
볡사
β€’
URL : /about
μ†Œκ°œ
Plain Text
볡사

μ‹€μŠ΅μ½”λ“œ

β€œκ²Œμ‹œνŒ ν”„λ‘œμ νŠΈβ€