v6 업데이트
npm install react-router-dom --save
<Router>
<Switch>
<Route exact path="/login" component={ Login } />
<LoginPage />
</Switch>
</Router>
<BrowserRouter>
<Routes>
<Route path="/login" element={<Login />} />
<Route path="/loginPage" element={<LoginPage />} />
</Routes>
</BrowserRouter>
<Route path="/users/:username" component={UsersPage} />
<Route path="/users/:username/*" element={<UsersPage />} />
<Route path="/optional/:value?" component={Optional} />
<Route path="/optional/:value?" element={<Optional />} />
<Route path="/optional" element={<Optional />} />
//App.js
<Route path="/users/:username" component={UsersPage} />
//UserPage.js
<Route path="/users/:username" component={UserMain} />
<Route path="/users/:username/about" component={About} />
//App.js
<Route path="/users/:username/*" element={<UsersPage />}>
<Route path="" element={<UserMain />} />
<Route path="about" element={<About />} />
</Route>
//UserPage.js
<Outlet />
<NavLink to="/messages" style={{ color: "blue"}} activeStyle={{ color: "green"}}>
Messages
</NavLink>
<NavLink to="/messages" className="nav-link" activeClassName="activated">
Messages
</NavLink>
<NavLink to="/messages" style={({ isActive }) => ({ color: isActive ? 'green' : 'blue' })}>
Messages
</NavLink>
<NavLink to="/messages" className={({ isActive }) => "nav-link" + (isActive ? " activated" : "") }>
Messages
</NavLink>
const history = useHistory();
history.push('/');
history.goback();
history.go(-2);
history.push(`/user/${user._id}`);
const navigate = useNavigate();
navigate('/');
navigate(-1);
navigate(-2);
navigate(`/user/${user._id}`);
const match = useRouteMatch();
console.log(match); // { path: '/', url: '/', isExact: true, params: {} }
<Link to={match.url} />; // 현재 url 로 이동
<Link to={`${match.url}/about`}>; // 현재 url에 /about을 붙인곳으로 이동
<Route path={match.patch} exact />
<Route path={`${match.patch}/about`} />
<Link to="" />; // 이렇게 입력시 현재 페이지로 이동
<Link to="about">; // 이렇게 입력시 현재 url에 /about을 붙인 곳으로 이동 *단 about앞에 /about을 붙이게되면 진짜 /about으로 이동되니 현재 기준으로 하려면 앞에 슬래쉬를 빼줘야함
<Route path="" exact />
<Route path="about" />