๐Ÿ“

11. React Router

ย 
๐Ÿ’ก
React Router v4/5 docs ๊ธฐ์ค€์ž…๋‹ˆ๋‹ค. v6์€ 2021๋…„ 11์›”์— ๋‚˜์™”์Šต๋‹ˆ๋‹ค.
ย 
React Router ์ฝ”๋“œ

์„ค์น˜ํ•˜๊ธฐ

App์„ ๋ณด๊ฒŒ๋˜๋ฉด ์ด 3๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๊ฒƒ๋“ค์„ ๋ชจ๋‘ ๋‹ค๋ฅธ ์ฃผ์ œ๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ์–ด ๋‹ค๋ฅธํŽ˜์ด์ง€์—์„œ ๋ณด์—ฌ์ฃผ๊ณ ์‹ถ์Šต๋‹ˆ๋‹ค.
notion imagenotion image
๋”ฐ๋ผ์„œ ์ง€๊ธˆ๊นŒ์ง€ ์ง„ํ–‰ํ•œ ๋‚ด์šฉ์„ ์—ฌ๋Ÿฌ ์ฃผ์†Œ๋กœ ๋‚˜๋ˆ„๊ณ  ๊ฐ ์ฃผ์†Œ๋ณ„๋กœ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
์šฐ๋ฆฌ๋Š” ํ˜„์žฌ 3๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ฃผ์†Œ๋ฅผ /hello, /resume, /time ์œผ๋กœ ๊ตฌ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
ย 
๋จผ์ € ๋ผ์šฐํŠธ ์„ค์ •์„ ์œ„ํ•ด react-router-dom ์„ ์„ค์น˜ํ•ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.
npm install react-router-dom@5.2.0
ย 
  • ์šฐ์„  ๊ฐ„๋‹จํ•˜๊ฒŒ ๋จผ์ € ์‹ค์Šตํ•ด๋ด…์‹œ๋‹ค.
import React from "react"; import { BrowserRouter, Route } from "react-router-dom"; function App() { return ( <BrowserRouter> {/* ๋ผ์šฐํŠธ๋ฅผ ๊ฐ์‹ธ์ค๋‹ˆ๋‹ค. */} <Route path="/" component={Index}/> <Route path="/one" component={One}/> <Route path="/two" component={Two}/> <Route path="/three" component={Three}/> </BrowserRouter> ); } function Index(){ return <h1>hello world0</h1> } function One(){ return <h1>hello world1</h1> } function Two(){ return <h1>hello world2</h1> } function Three(){ return <h1>hello world3</h1> } export default App;
notion imagenotion image

BrowserRouter

์šฐ๋ฆฌ๋Š” ๋ผ์šฐํŒ…์„ ์œ„ํ•ด BrowserRouter๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ผ์šฐํŠธ๊ฐ€ ๋™์ž‘ํ•  ๋ถ€๋ถ„์— ๊ฐ์‹ธ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๊ฐ ์ฃผ์†Œ๋ณ„๋กœ ๋ผ์šฐํŒ…์„ ํ•˜๊ธฐ ์œ„ํ•ด Route์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
<Route path="์ฃผ์†Œ" component={์ปดํฌ๋„ŒํŠธ}/> ์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ž‘์„ฑํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์— ์ฃผ์„์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค.
import React from 'react'; import {BrowserRouter, Route} from 'react-router-dom // ์‚ฌ์šฉํ•  ์ปดํฌ๋„ŒํŠธ ์ถ”๊ฐ€ํ•˜๊ธฐ import Hello from './Components/Hello' import Time from './Components/Time' import Resume from './Components/Resume' function App() { return ( <BrowserRouter>{/* ๋ผ์šฐํŠธ๋ฅผ ๊ฐ์‹ธ์ค๋‹ˆ๋‹ค. */} <Route path="/hello" component={Hello}/>{/* ๊ฐ๊ฐ ์ฃผ์†Œ์™€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž…๋ ฅํ•ด ์‚ฌ์šฉ */} <Route path="/time" component={Time}/> <Route path="/resume" component={Resume}/> </BrowserRouter> ); } export default App;
ย 

Route

์•ž์„œ ๋งŒ๋“  ๋ผ์šฐํŠธ์—์„œ์˜ "/", ์ฆ‰ ๋ฉ”์ธํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋ฉด ์•„๋ฌด๊ฒƒ๋„ ๋‚˜์˜ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ™ˆ ํ™”๋ฉด์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๋‚จ๊ฒจ๋’€๋Š”๋ฐ์š”. Components ํด๋”์— Home.js ํŒŒ์ผ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด๋‘๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
import React from 'react'; function Home(props) { return( <div> <h1>Home ํ™”๋ฉด!</h1> </div> ) } export default Home
Components/Home.js
ย 
App.js์—๋„ ๋ผ์šฐํŠธ๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
import React from 'react'; import {BrowserRouter, Route} from 'react-router-dom' import Hello from './Components/Hello' import Time from './Components/Time' import Resume from './Components/Resume' import Home from './Components/Home'; function App() { return ( <BrowserRouter> <Route path="/" component={Home}/> <Route path="/hello" component={Hello}/> <Route path="/time" component={Time}/> <Route path="/resume" component={Resume}/> </BrowserRouter> ); } export default App;
src/App.js return์— Route ์ถ”๊ฐ€ํ•˜๊ธฐ
ย 
์ด ์ƒํƒœ์—์„œ ์ฃผ์†Œ์— ์ ‘์†ํ•˜๋ฉด ํ™ˆ ํ™”๋ฉด์ด ์ œ๋Œ€๋กœ ๋‚˜์˜ต๋‹ˆ๋‹ค!
notion imagenotion image
๋‹ค์Œ '/hello' ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
notion imagenotion image
ย 
์ž˜ ๋‚˜์˜จ๊ฒƒ ๊ฐ™์€๋ฐ ํ™ˆ ํ™”๋ฉด์ด ๊ฐ™์ด ์ถœ๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํ˜„์ƒ์€ '/' ์ฃผ์†Œ์™€ '/hello'์˜ ์ฃผ์†Œ ์‚ฌ์ด์—์„œ '/' ์ด ๋ถ€๋ถ„์ด ์ค‘์ฒฉ๋˜์–ด ๋ฐœ์ƒํ•˜๋Š” ํ˜„์ƒ์ž…๋‹ˆ๋‹ค.
์ถ”๊ฐ€์˜ˆ์‹œ
Time ์ปดํฌ๋„ŒํŠธ์˜ path๋ฅผ โ€œ/hello/timeโ€์œผ๋กœ ์ˆ˜์ •ํ•ด๋ด…์‹œ๋‹ค.
import React from 'react'; import {BrowserRouter, Route} from 'react-router-dom' import Hello from './Components/Hello' import Time from './Components/Time' import Resume from './Components/Resume' import Home from './Components/Home'; function App() { return ( <BrowserRouter> <Route path="/" component={Home}/> <Route path="/hello/" component={Hello}/> <Route path="/hello/time" component={Time}/> <Route path="/resume" component={Resume}/> </BrowserRouter> ); } export default App;
src/App.jsdp Route ์ˆ˜์ •ํ•˜๊ธฐ
/hello/time ๋ฅผ ์ ‘์†ํ•˜๋ฉด /hello ์™€ /hello/time์ด ๋™์‹œ์— ์ถœ๋ ฅ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. '/hello' ๋ถ€๋ถ„์ด ๊ฒน์น˜๊ธฐ ๋•Œ๋ฌธ์ด์—์š”!
ย 

์ •ํ™•ํ•œ ๊ฒฝ๋กœ ์„ค์ • : exact

์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด Route์—๋Š” exact๋ผ๋Š” props๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. (Router๋„ ์ปดํฌ๋„ŒํŠธ!) ์ฃผ์†Œ๊ฐ€ ์ •ํ™•ํ•˜๊ฒŒ ์ผ์น˜ํ–ˆ์„ ๋•Œ๋งŒ ๋™์ž‘ํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ ์šฉํ•ด ๋ด…์‹œ๋‹ค.
// ๊ธฐ์กด์ฝ”๋“œ import React from "react"; import { BrowserRouter, Route } from "react-router-dom"; import Hello from "./Components/Hello"; import Time from "./Components/Time"; import Resume from "./Components/Resume"; import Home from "./Components/Home"; function App() { return ( <BrowserRouter> <Route path="/" exact component={Home} /> <Route path="/hello/" exact component={Hello} /> <Route path="/hello/time" exact component={Time} /> <Route path="/resume" exact component={Resume} /> </BrowserRouter> ); } export default App;
src/App.js

Component props ์ „๋‹ฌํ•˜๊ธฐ

ํ˜„์žฌ props๋ฅผ ์ „๋‹ฌํ•ด์•ผํ•  ์ปดํฌ๋„ŒํŠธ๋Š” Hello์™€ Resume์ž…๋‹ˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ์— props๋ฅผ ์ „๋‹ฌํ•ด๋ด…์‹œ๋‹ค.
1) Route์˜ render ์†์„ฑ ์‚ฌ์šฉํ•˜์—ฌ props ์ „๋‹ฌํ•˜๊ธฐ
2) ์ž์‹์œผ๋กœ ๋ Œ๋”ํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ์— props ์ „๋‹ฌํ•˜๊ธฐ
import React from "react"; import { BrowserRouter, Route } from "react-router-dom"; import Hello from "./Components/Hello"; import Time from "./Components/Time"; import Resume from "./Components/Resume"; import Home from "./Components/Home"; function App() { return ( <BrowserRouter> <Route path="/" exact component={Home} /> <Route path="/hello" exact render={() => <Hello name="๊ฐœ๋ฆฌ" />} /> <Route path="/time" exact component={Time} /> <Route path="/resume" exact> <Resume hello="Hello" name="๊ฐœ๋ฆฌ" hobby="๊ฒŒ์ž„" food="๊ณ ๊ธฐ" color="blue" /> </Route> </BrowserRouter> ); } export default App;
src/App.js
ย 

Link

<Link>
<Link to="/about">home</Link>
ย 
to ์†์„ฑ์—๋Š” ์ ‘๊ทผํ•  ๊ฒฝ๋กœ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์ด๋‚˜ ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋กœ ๊ฒฝ๋กœ๋ฅผ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด์— ๊ฒฝ์šฐ html ํƒœ๊ทธ์˜ <a> ์˜ ์†์„ฑ href ์— ๋„ฃ์–ด์ค€ ๊ฐ’์ฒ˜๋Ÿผ ์ ˆ๋Œ€๊ฒฝ๋กœ ๋˜๋Š” ์ƒ๋Œ€๊ฒฝ๋กœ๋ฅผ ๋ฌธ์ž์—ด์˜ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๊ฐ์ฒด๋กœ ๋„ฃ์–ด์ค„ ๊ฒฝ์šฐ pathname, search, hash, state ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’๋“ค์„ ๋„ฃ์–ด์„œ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
// Link ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ• // ๋ฌธ์ž์—ด <Link to="/courses?sort=name" /> // ๊ฐ์ฒด <Link to={{ pathname: "/courses", search: "?sort=name", hash: "#the-hash", state: { fromDashboard: true } }} />
ย 
ย 
aํƒœ๊ทธ๋ฅผ ์“ฐ์ง€์•Š๊ณ  link ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๊ฐ€ ๋ญ˜๊นŒ์š”?
aํƒœ๊ทธ๋Š” ํด๋ฆญํ–ˆ์„ ๊ฒฝ์šฐ, href์— ์„ค์ •ํ•ด์ค€ ๊ฒฝ๋กœ ์ด๋™๊ณผ ๋™์‹œ์— ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ๋•Œ๋ฌธ์— ํŽ˜์ด์ง€๊ฐ€ ์ƒˆ๋กœ๊ณ ์นจ์ด ๋ฉ๋‹ˆ๋‹ค.
react-router-dom์ด ์ œ๊ณตํ•˜๋Š” Link์˜ ๊ฒฝ์šฐ HTML5 History API๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ธŒ๋ผ์šฐ์ €์˜ ์ฃผ์†Œ๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ํŽ˜์ด์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ค์ง€ ์•Š๊ณ , dom๋งŒ ์กฐ์ž‘ํ•ด์„œ ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
ย 
import React from "react"; import { BrowserRouter, Route, Link } from "react-router-dom"; import Hello from "./Components/Hello"; import Time from "./Components/Time"; import Resume from "./Components/Resume"; import Home from "./Components/Home"; function App() { return ( <BrowserRouter> <Link to="" style={{ margin: "0 5px" }}> ํ™ˆ </Link> <Link to="/time" style={{ margin: "0 5px" }}> ํ˜„์žฌ์‹œ๊ฐ„ </Link> <Link to="/hello" style={{ margin: "0 5px" }}> ์•ˆ๋…• </Link> <Link to="/Resume" style={{ margin: "0 5px" }}> ์†Œ๊ฐœ </Link> <Route path="/" exact component={Home} /> <Route path="/hello" exact render={() => <Hello name="๊ฐœ๋ฆฌ" />} /> <Route path="/time" exact component={Time} /> <Route path="/resume" exact> <Resume hello="Hello" name="๊ฐœ๋ฆฌ" hobby="๊ฒŒ์ž„" food="๊ณ ๊ธฐ" color="blue" /> </Route> </BrowserRouter> ); } export default App;
src / App.js์— link๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
ํ™ˆ์„ ํด๋ฆญ : localhost:3000/
ํ˜„์žฌ์‹œ๊ฐ„์„ ํด๋ฆญ : localhost:3000/time
์†Œ๊ฐœ๋ฅผ ํด๋ฆญํ•˜๋ฉด : localhost:3000/resume
์•ˆ๋…•์„ ํด๋ฆญํ•˜๋ฉด : localhost:3000/hello
ย 

Switch

๊ธฐ์กด Route์— exact ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ๋„ ๋˜์ง€๋งŒ, exact ์˜ ๊ฒฝ์šฐ Route๋งˆ๋‹ค ๊ฐ๊ฐ ์„ค์ •์„ ํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค. <Switch>๋กœ Route๋“ค์„ ๊ฐ์‹ธ์ฃผ๋ฉด ์ฒซ๋ฒˆ์งธ๋กœ ๋งค์นญ๋˜๋Š” path (์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” โ€œ/โ€) ๋ฅผ ๊ฐ€์ง„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋ง์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์žฅ ์ƒ๋‹จ์— ์ปดํฌ๋„ŒํŠธ์—๋งŒ exact ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
import React from "react"; import { BrowserRouter, Route, Switch, Link } from "react-router-dom"; import Hello from "./Components/Hello"; import Time from "./Components/Time"; import Resume from "./Components/Resume"; import Home from "./Components/Home"; function App() { return ( <BrowserRouter> <Link to="" style={{ margin: "0 5px" }}> ํ™ˆ </Link> <Link to="/time" style={{ margin: "0 5px" }}> ํ˜„์žฌ์‹œ๊ฐ„ </Link> <Link to="/hello" style={{ margin: "0 5px" }}> ์•ˆ๋…• </Link> <Link to="/Resume" style={{ margin: "0 5px" }}> ์†Œ๊ฐœ </Link> <Switch> <Route path="/" exact component={Home} /> <Route path="/hello" render={() => <Hello name="๊ฐœ๋ฆฌ" />} /> <Route path="/time" component={Time} /> <Route path="/resume"> <Resume hello="Hello" name="๊ฐœ๋ฆฌ" hobby="๊ฒŒ์ž„" food="๊ณ ๊ธฐ" color="blue" /> </Route> </Switch> </BrowserRouter> ); } export default App;
src / App.js์— switch ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
ย 

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

ํ•˜๋‚˜์˜ ๊ฒฝ๋กœ์—์„œ ๋” ์„ธ๋ถ€์ ์œผ๋กœ ๊ฒฝ๋กœ๋ฅผ ๋‚˜๋ˆŒ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ผ์šฐํŒ…์ด ๋˜๊ณ  ๊ทธ ์•ˆ์—์„œ ๋ผ์šฐํŒ…์ด ํ•œ ๋ฒˆ ๋” ๋˜๋Š” ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค.
import React from "react"; import { BrowserRouter, Route, Switch, Link } from "react-router-dom"; import Hello from "./Components/Hello"; import Time from "./Components/Time"; import ResumeRouter from "./Components/ResumeRouter"; import Home from "./Components/Home"; function App() { return ( <BrowserRouter> <Link to="" style={{ margin: "0 5px" }}> ํ™ˆ </Link> <Link to="/time" style={{ margin: "0 5px" }}> ํ˜„์žฌ์‹œ๊ฐ„ </Link> <Link to="/hello" style={{ margin: "0 5px" }}> ์•ˆ๋…• </Link> <Link to="/resume" style={{ margin: "0 5px" }}> ์†Œ๊ฐœ </Link> <Switch> <Route path="/" exact component={Home} /> <Route path="/hello" render={() => <Hello name="๊ฐœ๋ฆฌ" />} /> <Route path="/time" component={Time} /> <Route path="/resume" children={ResumeRouter} /> </Switch> </BrowserRouter> ); } export default App;
src / App.js
import React from "react"; import { Route, Switch } from "react-router-dom"; import Resume from "./Resume"; const resumeId = ({ match }) => { console.log(match); return ( <> <div>path : {match.path}</div> </> ); }; const ResumeRouter = () => { return ( <Switch> <Route exact path="/resume/"> <Resume hello="Hello" name="๊ฐœ๋ฆฌ" hobby="๊ฒŒ์ž„" food="๊ณ ๊ธฐ" color="blue" /> </Route> <Route path="/resume/1" component={resumeId}></Route> <Route path="/resume/2" component={resumeId}></Route> <Route path="/resume/3" component={resumeId}></Route> </Switch> ); }; export default ResumeRouter;
src / Components / ResumeRouter.js
ย 
์œ„์— ์žˆ๋Š” ResumeRouter์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
๋‹ค๋ฅธ ์ ์€ Route๊ฐ€ Route์™€ ์—ฐ๊ฒฐ๋œ ์ปดํฌ๋„ŒํŠธ์— ์ž๋™์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฐ’์ธ match ์‚ฌ์šฉ์˜ ์œ ๋ฌด์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” path๋ฅผ ์ง€์ •ํ•  ๋•Œ /resume/ ๋ผ๊ณ  ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  props๋กœ ์ „๋‹ฌ๋˜๋Š” match ๊ฐ’์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.
import React from "react"; import { Route, Switch } from "react-router-dom"; import Resume from "./Resume"; const resumeId = ({ match }) => { console.log(match); return ( <> <div>path : {match.path}</div> </> ); }; const ResumeRouter = ({ match }) => { console.log("render"); return ( <Switch> {/* <Route exact path="/resume "></Route> */} <Route exact path={match.path} component={resumeId}></Route> <Route path={`${match.path}/1`} component={resumeId}></Route> <Route path={`${match.path}/2`} component={resumeId}></Route> <Route path={`${match.path}/3`} component={resumeId}></Route> </Switch> ); }; export default ResumeRouter;
src / Components / ResumeRouter.js

Route ์‚ฌ์šฉํ•˜์—ฌ ํŽ˜์ด์ง€ ๊ฒฝ๋กœ ๋งŒ๋“ค๊ธฐ

  • Home Page : /
  • Product Detail Page : /products/:id
    • ex) /products/1 , /products/2, /products/3, /products/4
  • Cart Page : /cart
  • Coupon Page : /users/coupon
  • Question Page : /users/question
  • Notice Page : /users/notice
  • User Page : /users
ย 
์‹คํ–‰ํ•ด๋ณด์„ธ์š”!
ย 
React Router V6
ย 
React Router V4/5
ย 

ReactRouter V6๊ฐ€ ๋˜๋ฉด์„œ ๋‹ฌ๋ผ์ง„ ๋ฌธ๋ฒ•๋“ค

๊ณต์‹๋ฌธ์„œ
ย 
  • ์—…๋ฐ์ดํŠธ(๋ฒ„์ „ ๋ช…์‹œ ์•ˆํ•ด์ฃผ์‹œ๋ฉด ๋ฒ„์ „ Up ๋ฉ๋‹ˆ๋‹ค.)
npm install react-router-dom

1. Routes

Switch๋Š” ์‚ฌ๋ผ์ง€๊ณ  Routes๊ฐ€ Switch๋ฅผ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค.
ย 
๊ธฐ์กด ์ฝ”๋“œ
<Switch> <Route path="/" exact component={Home} /> <Route path="/time" component={Time} /> </Switch>
ย 
v6์ฝ”๋“œ
<Routes> <Route path="/" exact component={Home} /> <Route path="/time" component={Time} /> </Routes>
ย 
ย 

2. exact๊ฐ€ ์‚ฌ๋ผ์ง€๋‹ค

V6๋ถ€ํ„ฐ๋Š” exact๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ Route์— ๋˜์–ด์žˆ์–ด์„œ ๋” ์ด์ƒ exact props๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ์ •ํ™•ํ•œ ๊ฒฝ๋กœ๋กœ ์ด๋™์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
ย 
๊ธฐ์กด ์ฝ”๋“œ
<Route path="/" exact component={Home} /> <Route path="/time" exact component={Time} />
ย 
v6์ฝ”๋“œ
<Route path="/" component={Home} /> <Route path="/time" component={Time} />
ย 

3. element

Route์— ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ children์ด๋‚˜ component๋ฅผ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ, v6๋ถ€ํ„ฐ๋Š” element๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
Route์™€ ์—ฐ๊ฒฐ๋œ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ํ•„์š”ํ•œ props๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋„ ๊ธฐ์กด์ฝ”๋“œ์™€ ๊ฐ™์ด ์ „๋‹ฌํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด JSX๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ์ „๋‹ฌํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๊ธฐ์กด์ฝ”๋“œ
<Route path="/" exact component={Home} /> <Route path="/hello" render={() => <Hello name="๊ฐœ๋ฆฌ" />} /> <Route path="/time" component={Time} /> <Route path="/resume"> <Resume hello="Hello" name="๊ฐœ๋ฆฌ" hobby="๊ฒŒ์ž„" food="๊ณ ๊ธฐ" color="blue" /> </Route>
ย 
v6์ฝ”๋“œ
<Route path="/" element={<Home />} /> <Route path="/hello" element={<Hello name="๊ฐœ๋ฆฌ" />} /> <Route path="/time" element={<Time />} /> <Route path="/resume" element={ <Resume hello="Hello" name="๊ฐœ๋ฆฌ" hobby="๊ฒŒ์ž„" food="๊ณ ๊ธฐ" color="blue" /> } ></Route>
ย 

4. Route์˜ ๋ถ€๋ชจ๋กœ Routes

๊ธฐ์กด์— v5์—์„œ๋Š” swtch๋กœ Route๋ฅผ ๊ฐ์‹ธ๋Š” ๊ฒƒ์€ ์„ ํƒ์‚ฌํ•ญ์ด์—ˆ๋‹ค๋ฉด v6์—์„œ switch์˜ ๋Œ€์ฒด ์—ญํ• ์ธ Routes๋Š” ๋ฐ˜๋“œ์‹œ Route๋ฅผ ๊ฐ์‹ธ์•ผํ•˜๋ฉฐ ๋ฐ˜๋“œ์‹œ Route๋Š” Routes์˜ ์ง์†์ž์‹์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
ย 
๊ธฐ์กด์ฝ”๋“œ
<Route path="/" exact component={Home} /> <Route path="/hello" exact render={() => <Hello name="๊ฐœ๋ฆฌ" />} /> <Route path="/time" exact component={Time} />
ย 
v6์ฝ”๋“œ
<Routes> <Route path="/" element={<Home />} /> <Route path="/hello" element={<Hello name="๊ฐœ๋ฆฌ" />} /> <Route path="/time" element={<Time />} /> </Routes>
ย 

5. ์ค‘์ฒฉ๋œ ๋ผ์šฐํ„ฐ

ย 
v6์ฝ”๋“œ
๋ฐฉ๋ฒ•1 /resume/ ๋‹ค์Œ์— ๋” ์ƒ์„ธ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•  ๋•Œ ์•„๋ž˜์™€ ๊ฐ™์ด Route์•ˆ์— Route ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
element๋กœ ์ „๋‹ฌ๋˜๋Š” ์ปดํฌ๋„ŒํŠธ์—๋Š” ๋ฆฌ์•กํŠธ ๋ผ์šฐํ„ฐ์—์„œ ์ œ๊ณตํ•˜๋Š” <Outlet/> ์ปดํฌ๋„ŒํŠธ๋ฅผ returnํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
import React from "react"; import { BrowserRouter, Route, Routes, Link, useParams, Outlet, } from "react-router-dom"; import Resume from "./Components/Resume"; const ResumeId = () => { return ( <> <Outlet /> </> ); }; function App() { return ( <BrowserRouter> <Routes> <Route path="resume/*" element={<ResumeId />}> <Route path="" element={ <Resume hello="Hello" name="๊ฐœ๋ฆฌ" hobby="๊ฒŒ์ž„" food="๊ณ ๊ธฐ" color="blue" /> } ></Route> </Route> </Routes> </BrowserRouter> ); } export default App;
ย 
๋ฐฉ๋ฒ•2 ์ง์ ‘๊ธฐ์žฌํ•ด์ค๋‹ˆ๋‹ค.
const ResumePage = () => { return ( <div> <Routes> <Route path=":id" element={<ResumeId />}></Route> <Route path="/" element={ <Resume hello="Hello" name="๊ฐœ๋ฆฌ" hobby="๊ฒŒ์ž„" food="๊ณ ๊ธฐ" color="blue" /> } ></Route> </Routes> </div> ); }; const ResumeId = () => { const a = useParams(); return ( <> <div>{a.id}</div> </> ); }; function App() { return ( <BrowserRouter> <Link to="" style={{ margin: "0 5px" }}> ํ™ˆ </Link> <Link to="/time" style={{ margin: "0 5px" }}> ํ˜„์žฌ์‹œ๊ฐ„ </Link> <Link to="/hello" style={{ margin: "0 5px" }}> ์•ˆ๋…• </Link> <Link to="/Resume" style={{ margin: "0 5px" }}> ์†Œ๊ฐœ </Link> const ResumeId = () => { const a = useParams(); return ( <> <div>{a.id}</div> </> ); }; </BrowserRouter> ); }
ย