npm install react-router-dom or npm install react-router-dom@6 // 6๋ฒ์ - ํน์ ๋ฒ์ ์ค์น
โ package.json ํ์ผ ํ์ธ
โ
src/index ํ์ผ
โ๏ธ import { BrowserRouter } from "react-router-dom";
โ๏ธ ์ต์์ indexํ์ผ์์ BrowserRouter ์ปดํฌ๋ํธ๋ก App์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ผ๋ค.
โ๏ธ Path์ ์
๋ ฅํ ๊ฒฝ๋ก์ ๋ง์ ๊ฒฝ์ฐ element ์ปดํฌ๋ํธ๋ฅผ ๋ณด์ฌ์ฃผ์ธ์ ๋ผ๊ณ ์๊ฐ์ ํ๋ฉด ๋!
EX)
<Route path="/about" element={<About />}>
url ๐ http://localhost:3000/about
๋ณด์ฌ์ง๋ ์ปดํฌ๋ํธ ๐ About ์ปดํฌ๋ํธ
// App.js
import {Routes, Route} from 'react-router-dom';
import RouterMain from './components/router/RouterMain';
import RouterAbout from './components/router/RouterAbout';
import RouterPage from './components/router/RouterPage';
function App() {
return (
<div className="App">
<Routes>
<Route path="/" element={ <RouterMain /> }/>
<Route path="/about" element={ <RouterAbout />} />
<Route path="/page" element={ <RouterPage />} />
</Routes>
</div>
);
}
export default App;
- - - - - - - - - - - - - - - - - -
๐ url ์
๋ ฅ ์
http://localhost:3000 ๐ RouterMain ์ปดํฌ๋ํธ ๋
ธ์ถ
http://localhost:3000/about ๐ RouterAbout ๋
ธ์ถ
http://localhost:3000/page ๐ RouterPage ๋
ธ์ถ
์ ์์ ์ผ๋ก url์ ๋ง๊ฒ ๋์ค๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
โ ๏ธ ์๋ชป๋ ๊ฒฝ๋ก์ผ ๊ฒฝ์ฐ ๊ฒฝ๊ณ ๋ฌธ.
EX) localhost:3000/test
// path="*" RouterError ์ปดํฌ๋ํธ
<Route path="*" element={ <RouterError /> }/> {/* ์๋ ๊ฒฝ๋ก */}
โ๏ธ path="*" ์๋ชป๋ url์ ๊ฒฝ์ฐ Error ์ปดํฌ๋ํธ๊ฐ ๋ณด์ฌ์ง๋๋ก ์๋ฃ!
๐Link
โ EX)
import {Routes, Route, Link} from 'react-router-dom';
import RouterMain from './components/router/RouterMain';
import RouterAbout from './components/router/RouterAbout';
import RouterPage from './components/router/RouterPage';
import RouterError from './components/router/RouterError';
function App() {
return (
<div className="App">
{/* ๐ ๋งํฌ */}
<div className="link">
<Link to={"/"} >Main</Link>
<Link to={"/about"} >about</Link>
<Link to={"/page"} >page</Link>
</div>
<Routes>
<Route path="/" element={ <RouterMain /> }/>
<Route path="/about" element={ <RouterAbout />} />
<Route path="/page" element={ <RouterPage />} />
<Route path="*" element={ <RouterError /> }/> {/* ์๋ ๊ฒฝ๋ก */}
</Routes>
</div>
);
}
๐ link aํ๊ทธ ํด๋ฆญ ์ url์ ๋ง๋ ์ปดํฌ๋ํธ๋ก ๋ฐ๋๊ณ ์๋ ๊ฒ์ ์ ์ ์์ต๋๋ค!
import {Routes, Route, useNavigate} from 'react-router-dom';
const navigate = useNavigate(); // ๐
return (
<div className="App">
{/* ๋งํฌ */}
<div className="link">
<button type="button" onClick={()=> navigate("/") }>Main</button>
<button type="button" onClick={()=> navigate("/about")}>about</button>
<button type="button" onClick={()=> navigate("/page")}>page</button>
</div>
<Routes>
<Route path="/" element={ <RouterMain /> }/>
<Route path="/about" element={ <RouterAbout />} />
<Route path="/page" element={ <RouterPage />} />
<Route path="*" element={ <RouterError /> }/> {/* ์๋ ๊ฒฝ๋ก */}
</Routes>
</div>
);
โ๏ธ ์ ์์ ์ผ๋ก ๋์ ํ์ธ
function aboutClick() {
navigate("/about")
}
<button type="button" onClick={()=> aboutClick() }>about</button>
โ๏ธ ํจ์ ๋ด์ ์ ๋ ฅํ์ฌ ์กฐ๊ฑด์ ๋ฐ๋ผ ์คํํ๋๋ก ํ ์ ์์ต๋๋ค.
// -1 ๋ค๋ก ๊ฐ๊ธฐ -2๋ฅผ ์
๋ ฅํด๋ ๊ฐ๋ฅ - ๊ทธ์ ๋จ๊ณ๋ก ๋์๊ฐ๋ค.
navigate(-1)
โ๏ธ ๋ค๋ก ๊ฐ๊ธฐ -1
// โ๏ธ Route ์์์ผ๋ก Route๋ฅผ ์
๋ ฅํ๋ฉด ๋!
<Route path="/page" element={ <RouterPage /> } >
<Route path="detail" element={ <RouterDetail /> } />
</Route>
์์์ผ๋ก ๋ค์ด๊ฐ Route๋ path์ /page/ ์ดํ์ ์
๋ ฅ๋ url์ ์
๋ ฅํ๋ฉด ๋ฉ๋๋ค!
detail๋ง ์์ฑํ๋ฉด /page/detail ๊ฒฝ๋ก๊ฐ ์ง์ ๋ฉ๋๋ค.
๋ผ์ฐํฐ๋ก ๊ฒฝ๋ก, ์ปดํฌ๋ํธ ๋ฑ๋ก์ด ๋๋ฌ์ต๋๋ค!
/page/detail ๊ฒฝ๋ก๋ก ์ ๊ทผํ ๊ฒฝ์ฐ ๊ฒฝ๊ณ ๋ ๋์ค์ง ์๋๋ฐ
detail์ ์ ๋ณด๋ ๋ณด์ฌ์ง์ง ์๊ณ page ์ปดํฌ๋ํธ ๋ด์ฉ๋ง ๋์ค๊ฒ ๋ฉ๋๋ค.
EX)
/page
/page/detail
โ๏ธ ๊ฐ์ ๋ด์ฉ ๋
ธ์ถ
๊ฒฝ๋ก๋ ๋ฐ๋์๋๋ฐ ๋ ๋๋ง ๋์ง ์๋ ์ด์ ๋ page ํ์ detail ์ปดํฌ๋ํธ๋ฅผ ๋ถ๋ฌ์ค๋ ์์น, ๊ณต๊ฐ์ ์ก์์ฃผ์ง ์์์ ์ ๋๋ค!
โ ์ถ๊ฐ๋ก page์ detail๋ฅผ ๋ถ๋ฌ์ค๊ธฐ ์ํด Outlet์ ์ฌ์ฉ!
import { Outlet } from "react-router-dom";
<Outlet />
// Route ๐ /page ํ์๋ก detail, board ์ถ๊ฐ
<Route path="/page" element={ <RouterPage /> } >
<Route path="detail" element={ <RouterDetail /> } />
<Route path="board" element={ <RouterBoard /> } />
</Route>
// RouterPage.jsx
<div className="page">
<h1>RouterPage ์ปดํฌ๋ํธ ์
๋๋ค.</h1>
<hr />
{/* ๐ Route page ํ์ ์ปดํฌ๋ํธ ๋ถ๋ฌ์ค๊ธฐ */}
<Outlet />
</div>
โ
๊ฒฐ๊ณผ
๊ฒฝ๋ก: /page
/page ์ปดํฌ๋ํธ ๋ด์ฉ๋ง ๋์ค๊ฒ ๋ฉ๋๋ค.
๊ฒฝ๋ก: /page/detail
page ์ปดํฌ๋ํธ ๋ด์ฉ ๋ฐ detail ์ปดํฌ๋ํธ ๋ด์ฉ
๊ฒฝ๋ก: /page/board
page ์ปดํฌ๋ํธ ๋ด์ฉ ๋ฐ board ์ปดํฌ๋ํธ ๋ด์ฉ
โ ๊ธฐ์กด๊ณผ ์๋ก์ด Router ์ฝ๋ ๋น๊ต
// Configure nested routes with JSX
createBrowserRouter(
createRoutesFromElements(
<Route path="/" element={<Root />}>
<Route path="contact" element={<Contact />} />
<Route
path="dashboard"
element={<Dashboard />}
loader={({ request }) =>
fetch("/api/dashboard.json", {
signal: request.signal,
})
}
/>
<Route element={<AuthLayout />}>
<Route
path="login"
element={<Login />}
loader={redirectIfUser}
/>
<Route path="logout" action={logoutUser} />
</Route>
</Route>
)
);
// โญ Or use plain objects โญ
createBrowserRouter([
{
path: "/",
element: <Root />,
children: [
{
path: "contact",
element: <Contact />,
},
{
path: "dashboard",
element: <Dashboard />,
loader: ({ request }) =>
fetch("/api/dashboard.json", {
signal: request.signal,
}),
},
{
element: <AuthLayout />,
children: [
{
path: "login",
element: <Login />,
loader: redirectIfUser,
},
{
path: "logout",
action: logoutUser,
},
],
},
],
},
]);
// ์ถ์ฒ - https://reactrouter.com/en/main/start/overview
src/index.js
๐๊ธฐ์กด
๐์๋ก์ด
โ๏ธ BrowserRouter ๋์ RouterProvider ์ฌ์ฉ.
โ๏ธ props๋ก router={router}๋ฅผ ์
๋ ฅํฉ๋๋ค
โ๏ธ ๊ธฐ์กด Routes Route ๋์ router.jsx ํ์ผ์ ๋ง๋ค์ด ๋ถ๋ฆฌํ์ฌ ๊ด๋ฆฌ๋ฅผ ํฉ๋๋ค!
// src/index.js ์ฐธ๊ณ
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import reportWebVitals from './reportWebVitals';
import { RouterProvider } from 'react-router-dom';
import router from './router/router';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
๐๊ธฐ์กด Route
๐ ์๋ก์ด - object๋ก ์์ฑํฉ๋๋ค.
//EX)
import { createBrowserRouter } from 'react-router-dom';
const router = createBrowserRouter([
{
path:'/',
element:'<App />'
}
export default router;
// router.jsx EX)
import { createBrowserRouter } from 'react-router-dom';
import App from '../App';
import RouterMain from '../components/router/RouterMain';
import RouterAbout from '../components/router/RouterAbout';
import RouterPage from '../components/router/RouterPage';
import RouterDetail from '../components/router/RouterDetail';
import RouterBoard from '../components/router/RouterBoard';
const router = createBrowserRouter([
{
path:"/",
element: <App />,
children:[
{
index: true, // ๐ App Outlet default ์ปดํฌ๋ํธ
// path:"/", ๐ path:'/' ์
๋ ฅํด๋ App Outlet ๋ ๋๋ง
element:<RouterMain />
},
{
path:"about",
element:<RouterAbout />
},
{
path:"page",
element:<RouterPage />,
children: [
{
path:"detail",
element:<RouterDetail />
},
{
path:"board",
element:<RouterBoard />
}
]
},
]
},
])
export default router;
โ
Route ์์ฑ์ด ์๋ฃ๋์์ผ๋ ์ด์ App ๊ธฐ์ค์ผ๋ก children์
๋ณด์ฌ์ฃผ๊ธฐ ์ํด App.js์ Outlet์ ์ฌ์ฉํฉ๋๋ค!
// App.js
import './App.scss';
import {useNavigate, Outlet} from 'react-router-dom';
function App() {
const navigate = useNavigate();
return (
<div className="App">
<div className="link">
<button type="button" onClick={()=> navigate("/")}>Main</button>
<button type="button" onClick={()=> navigate("/about")}>about</button>
<button type="button" onClick={()=> navigate("/page")}>page</button>
<button type="button" onClick={()=> navigate("/page/detail")}>detail</button>
<button type="button" onClick={()=> navigate("/page/board")}>board</button>
</div>
{/* ๐Outlet ์ถ๊ฐ App-> children */}
<Outlet />
</div>
);
}
export default App;
โ ๋
๊ฐ์ฌํฉ๋๋ค. ๐