Router
Router
React Project์ Page Navigation์ ๋์์ฃผ๋ react-router-dom ์ด๋ผ๋ ๋ชจ๋์ด ์๋ค.
npm install react-router-dom
react-router-dom
import { HashRouter, Route } from "react-router-dom";
//...
<HashRouter>
<Route path="/" component={Home}></Route>
<Route path="/about" component={About}></Route>
</HashRouter>
//...
๋ผ์ฐํฐ๋ path , component 2๊ฐ์ Props์ ๊ฐ์ง๋ค.
- path : uri์ ๊ฐ์ผ๋ฉฐ ๋๋ฉ์ธ ์ฃผ์์์ ๋ค๋น๊ฒ์ด์ ํ๋ ์ฃผ์๋ฅผ ์ ๋ ฅํ๋ค.
- component : component๋ navaigation ์ ๋ถ๋ฌ์ค๋ component๋ฅผ ์ ๋ ฅํ๋ค.
โ/aboutโ ๊ฒฝ๋ก๋ก ์ ๊ทผํ์ ๊ฒฝ์ฐ๋ โ/โ์ ๋๋๋ง ์ํจ๋ค.
์ด์ ๋ ๋ฆฌ์ํธ ์์คํ
์์ฒด๊ฐ ์๊ธฐ ์์ ์ ์ฐพ๊ธฐ ๋๋ฌธ์ด๋ค(?)
ํด๊ฒฐ๋ฐฉ๋ฒ
- ์์ฑ๊ฐ์ผ๋ก exact={true} ์ฌ์ฉ์ ์๊ธฐ ์์ ์ ํจ์ค์ผ๋๋ง ๋๋๋ง ์ํจ๋ค.
Navigation
<a href=โ/โ> or <a href=โ/aboutโ> ์ ์๋์ด ์๋ ๊ฒ์ด๋ค.
๋ฆฌ์ํธ์์๋ <Link>๋ฅผ ์ฌ์ฉํด์ค์ผํ๋ฉฐ html์ด ์๋๊ธฐ ๋๋ฌธ์ href๊ฐ ์๋ to๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
Link๋ ๋ผ์ฐํฐ ์์์๋ง ์๋ํ๋ค.
import { Link } from "react-router-dom";
//...
<div>
<Link to="/">
<Link to="/about">
</div>
//...
Leave a comment