SPA ๊ธฐ๋ฒ์ ๊ทธ๋๋ก ์ ์งํ ์ํ๋ก REACT์์ ํ์ด์ง ์ด๋ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ผ๋ก React Router๊ฐ ์ฌ์ฉ๋๋ค.
๋ผ์ฐํ ์ด๋?
- ์ฌ์ฉ์๊ฐ ์์ฒญํ URL์ ๋ฐ๋ผ ํด๋น URL์ ๋ง๋ ํ์ด์ง๋ฅผ ๋ณด์ฌ์ฃผ๋ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ์ฝ๋ค.
- ๋ฆฌ์กํธ์๋ ๋ผ์ฐํ ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์กด์ฌํ๋๋ฐ, ๊ทธ ์ค ๋ฆฌ์กํธ ๋ผ์ฐํฐ(React Router)๋ฅผ ์ฌ์ฉํด๋ณด๋ ค๊ณ ํ๋ค.
React Router(๋ฆฌ์กํธ ๋ผ์ฐํฐ)๋ ๋ฌด์์ธ๊ฐ?
- ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ฃผ์๋ฅผ ๊ฐ์งํ๋ ์ญํ ์ ํ๋ฉฐ, ์ฌ๋ฌ ํ๊ฒฝ์์ ๋์ํ ์ ์๋๋ก ์ฌ๋ฌ ์ข ๋ฅ์ ๋ผ์ฐํฐ ์ปดํฌ๋ํธ๋ฅผ ์ ๊ณตํ๋ค.
์ข ๋ฅ
- BrowserRouter: HTML5๋ฅผ ์ง์ํ๋ ๋ธ๋ผ์ฐ์ ์ ์ฃผ์๋ฅผ ๊ฐ์งํ๋ค.
- HashRouter: ํด์ ์ฃผ์๋ฅผ ๊ฐ์งํ๋ค.
React Router(๋ฆฌ์กํธ ๋ผ์ฐํฐ) ์ฌ์ฉํด๋ณด๊ธฐ! (โ React Router v6)
1) ํ๋ก์ ํธ ์์ฑ
$ yarn create react-app router-test
2) ํด๋น ํ๋ก์ ํธ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
$ yarn add react-router-dom
3) app.js ํ์ผ์ ๊ฒฝ๋ก ๊ตฌ์ฑ
import React from 'react';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import './App.css';
import Home from './components/home';
import MyPage from './components/mypage';
function App() {
return (
<BrowserRouter>
<Routes>
// '/'๋ก ์ ๊ทผํ ๊ฒฝ์ฐ Home ์ปดํฌ๋ํธ๋ก ๋ ๋๋ง
<Route path='/' exact element={<Home />} />
<Route path='/home' element={<Home />} />
<Route path='/mypage' element={<MyPage />} />
</Routes>
</BrowserRouter>
);
}
export default App;
- BrowserRouter๋ฅผ ์ฌ์ฉํ์ฌ ํ ์คํธ ํด๋ณผ ์์ ์ด๋ฏ๋ก, < BrowserRouter> ํ๊ทธ๋ก ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ธ์ค๋ค.
- <Routes> ์ปดํฌ๋ํธ๋ ์ฌ๋ฌ <Route>๋ฅผ ๊ฐ์ธ์ ๊ทธ ์ค ๊ฒฝ๋ก๊ฐ ์ผ์นํ๋ ๋จ ํ๋๋ง ๋ ๋๋ง ์์ผ์ฃผ๋ ์ญํ ์ ํ๋ค.
- <Route>๋ path ์์ฑ์ ๊ฒฝ๋ก, element ์์ฑ์ ๋ณด์ฌ์ฃผ๊ณ ์ ํ๋ ์ปดํฌ๋ํธ๋ฅผ ๋ฃ์ด์ค๋ค.
(1) home.jsx
import React from 'react';
const Home = (props) => {
return (
<>
<h1>Home</h1>
<button>Go to MyPage</button>
</>
);
};
export default MyPage;
(2) mypage.jsx
import React from 'react';
const MyPage = (props) => {
return (
<>
<h1>MyPage</h1>
<button>Go to Home</button>
</>
);
};
export default MyPage;
4) Navigation(ํ์ด์ง ์ด๋)
(1) useNavigate
ex) home.jsx (mypage.jsx๋ ๋์ผํ๊ฒ ์์ )
import React from 'react';
import { useNavigate } from 'react-router-dom';
const Home = (props) => {
const navigate = useNavigate();
return (
<>
<h1>Home</h1>
<button
onClick={() => {
navigate('/mypage');
}}
>
Go to MyPage
</button>
</>
);
};
export default Home;
(2) Link
import React from 'react';
import { BrowserRouter, Route, Routes, Link } from 'react-router-dom';
import './App.css';
import Home from './components/home';
import MyPage from './components/mypage';
function App() {
return (
<BrowserRouter>
<nav>
<Link to='/home'>Home</Link>
<Link to='/mypage'>MyPage</Link>
</nav>
<Routes>
<Route path='/' exact element={<Home />} />
<Route path='/home' element={<Home />} />
<Route path='/mypage' element={<MyPage />} />
</Routes>
</BrowserRouter>
);
}
export default App;
5) Result
(1) http://localhost:3000/home
(2) http://localhost:3000/mypage
Reference
https://reactrouter.com/docs/en/v6/upgrading/v5
'Frontend > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React/Axios] ๐คฌ Axios message: 'Network Error'(CORS ์ค๋ฅ) (0) | 2022.10.30 |
---|---|
[React/Axios] axios delete ์์ฒญ ์ body์ data ๋ฃ๋ ๋ฐฉ๋ฒ (0) | 2022.10.20 |
[React] SPA(Single Page Application) ์ด๋? (0) | 2022.07.27 |
[React] React.PureComponent(์ฑ๋ฅ ํฅ์) (0) | 2022.07.03 |
[React] React ์ฌ์ฉํ ๋ ์๊ณ ์์ผ๋ฉด ์ข์ ๊ฒ! (1) | 2022.07.01 |