공식 문서 참고 https://reactrouter.com/web/guides/quick-start
yarn add react-router-dom
App.js
맛보기import React from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Home from "pages/Home";
function App() {
return (
<Router>
<Switch>
<Route exact path="/" component={Home} />
</Switch>
</Router>
);
}
export default App;
exact
을 쓰면 location.pathname === path
되어야 매칭한다
보통 Switch
바깥에 navigation을 둬서 쉽게 페이지를 제어하도록 한다. 혹은 Header
Link
활용import React from "react";
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
<Router>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/topics">Topics</Link>
<Switch>
<Route exact path="/" component={() => <h1> home </h1>} />
<Route path="/about" component={() => <h1> about</h1>} />
<Route path="/topics" component={() => <h1> topics</h1>} />
</Switch>
</Router>