React Router
SPA (Single Page Application)
ํ์ด์ง๊ฐ ํ ๊ฐ์ธ ์ดํ๋ฆฌ์ผ์ด์ .
- react๋ ํ๋์ htmlํ์ผ์ ๋ ๋๋งํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํ๋จ
- ํ๋์ htmlํ์ผ์ ์ฌ๋ฌ ๊ฐ์ ํ์ด์ง๋ฅผ ๋ณด์ฌ์ฃผ๋ ๋ฐฉ๋ฒ์ด
Routing
โ Routing
๋ค๋ฅธ ๊ฒฝ๋ก(url ์ฃผ์)์ ๋ฐ๋ผ ๋ค๋ฅธ View(ํ๋ฉด)์ ๋ณด์ฌ์ฃผ๋ ๊ฒ
- react ์์ฒด์๋ ์ด๋ฐ ๊ธฐ๋ฅ์ด ๋ด์ฅ๋์ด ์์ง ์์ ๋ณ๋๋ก ์ค์นํด์ผ ํ๋ค
react
๊ฐframework
๊ฐ ์๋library
๋ก ๋ถ๋ฅ๋๋ ์ด์React-router
react์ routing ๊ธฐ๋ฅ์ ์ํด ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ library
โ React-router
๐ react-router ์ค์น
- โnpm install react-router-dom โsaveโ
--save
package.jsonํ์ผ์ dependencies์ ์ถ๊ฐ๋๋๋ก ํ๋ ๋ช ๋ น์ด
๐ Routes component ๊ตฌํ
- Routes.js๋ฅผ ๋ง๋ค์ด์ ์ฐ๊ฒฐํด์ผ ํ๋ component๋ฅผ
<Router>
์๋์ ์์น์ํค๊ธฐ<Router>
์๋์ ์์น ์ํจ๋ค๋ ๋ง์,<Router>
๊ธฐ๋ฅ์ ์ถ๊ฐํ๊ฒ ๋ค๋ผ๋ ๋ป
import React from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
// import Component๋ช
from 'file ๊ฒฝ๋ก';
import Login from "./pages/Login/Login";
import Signup from "./pages/Signup/Signup";
import Main from "./pages/Main/Main";
class Routes extends React.Component {
render() {
return (
<Router>
<Switch>
<Route exact path='/' component={Login} />
<Route exact path='/signup' component={Signup} />
<Route exact path='/main' component={Main} />
</Switch>
</Router>
);
}
}
export default Routes;
๐ index.js
ReactDOM.render(<Routes />, document.getElementById('root'));
๐ Route ์ด๋
<Link>
ํ๋ก์ ํธ ํ์ผ ๋ด, ์กฐ๊ฑด์์ด ์ด๋ํ ๋ ์ฌ์ฉ<Link to="/signup">ํ์๊ฐ์ </Link>
withRouterHOC
์กฐ๊ฑด์ด ํ์ํ ๊ฒฝ์ฐ ์ฌ์ฉ
import React from "react";
import { withRouter } from "react-router-dom";
class Login extends React.Component {
goToMain = () => {
this.props.history.push("/main");
};
render() {
return (
<div>
<button className='loginBtn' onClick={this.goToMain}>
๋ก๊ทธ์ธ
</button>
</div>
);
}
}
export default withRouter(Login);
โจ react-router-dom v6 ๋ณ๊ฒฝ์ฌํญ
์ด๋ ์๊ฐ๋ถํฐ
Switch๋ฅผ react-router-dom ํจํค์ง์์ import ํ ์ ์๋ค
๋ ์ค๋ฅ๊ฐ ์๊ฒผ๋๋ฐ, ์๊ณ ๋ณด๋react-router-dom v6
์์Switch
๊ฐRoutes
๋ก ๋ฐ๋์๋ค๊ณ ํ๋ค.
โ๏ธ Switch โก๏ธ Routes๋ก ๋ณ๊ฒฝ๋จ
// v5
<Switch>
<Route ... />
</Routes>
// v6
<Routes>
<Route ... />
</Routes>
โ๏ธ exact ์ต์ ์ญ์
- path ๋ฅผ ๊ธฐ์กด์
path="/Web/:id"
์์path=":id"
๋ก, ์๋๊ฒฝ๋ก๋ก ์ง์ - ์ด ์ธ์๋,
path="."
,path=".."
๋ฑ์ผ๋ก ์๋๊ฒฝ๋ก๋ฅผ ํํ
// categores ๋ก ์์๋๋ ๋ชจ๋ ๋ผ์ฐํ
๋งค์นญ
<Route path='categories/*' />
โ๏ธ route ์์ ์ปดํฌ๋ํธ ๋ ๋๋ง
// v5 ๋ฒ์ ์ฌ์ฉ ์์
<Route exact path="/" component={Login} />
)}
/>
// v6 ๋ฒ์ ์ฌ์ฉ ์์
<Route path="/" component={<Login />} />
โ๏ธ useHistory โก๏ธ useNavigate hook ๋ณ๊ฒฝ
// v5
const history = useHistory();
history.push('/home');
history.replace('/home');
// v6
const navigate = useNavigate();
navigate('/home');
navigate('/home', {replace: true});
// v6 ์์์ ์์ผ๋ก, ๋ค๋ก ๊ฐ๊ธฐ ์ฌ์ฉ๋ฐฉ๋ฒ ๋ณํ
<button onClick={() => navigate(-2)}>Go 2 pages back</button>
<button onClick={() => navigate(-1)}>Go back</button>
<button onClick={() => navigate(1)}>Go forward</button>
<button onClick={() => navigate(2)}>Go 2 pages forward</button>
โ๏ธ v6 ์ค์ฒฉ ๋ผ์ฐํ
import { BrowserRouter, Routes, Route, Link, Outlet } from "react-router-dom";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path='/' element={<Home />} />
<Route path='users' element={<Users />}>
<Route path='me' element={<OwnUserProfile />} />
<Route path=':id' element={<UserProfile />} />
</Route>
</Routes>
</BrowserRouter>
);
}
function Users() {
return (
<div>
<nav>
<Link to='me'>My Profile</Link>
</nav>
<Outlet />
</div>
);
}
โ๏ธ ์ฝ๋๋ก ํ์ธํ๊ธฐ
import React from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Login from "./pages/Login/Login";
import Signup from "./pages/Signup/Signup";
import Main from "./pages/Main/Main";
class Routes extends React.Component {
render() {
return (
<Router>
<Routes>
<Route path='/' component={<Login />} />
<Route path='/signup' component={<Signup />} />
<Route path='/main' component={<Main />} />
</Routes>
</Router>
);
}
}
โ ๋ชฉํ!
- SPA์ ๊ฐ๋ ์ ๋ํด ์ดํดํ๊ณ
router
๊ฐ ๋ฌด์์ด๊ณ ์ ์จ์ผ ํ๋์ง,router
์ฌ์ฉ ๋ฐฉ๋ฒ์ ๋ํด ์ตํ์