์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- ํ๋ค์ค
- Pandas
- Streamlit๊ธฐ๋ณธ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ํ์ด์ฌ๊ณต๋ถ
- ์๋๋ก์ด๋ ์คํ๋์ค
- RESTful API
- Android
- ์๋ฐ์ด๋ณด
- ์น๋์๋ณด๋ ๊ธฐ์ด
- ์๋ฐ์คํฌ๋ฆฝํธ ๊ณต๋ถ
- ์คํธ๋ฆผ๋ฆฟ ๊ธฐ๋ณธ
- ์๋ฐ๊ณต๋ถ
- MySQL
- streamlit
- ํ๋ค์ค๊ณต๋ถ
- JavaScript
- ์๋ฐ
- ์๋ฐ๊ธฐ์ด
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์๋ฐํ๋ก๊ทธ๋๋ฐ
- serverless
- ์๋๋ก์ด๋ ์ฑ ๊ฐ๋ฐ
- ์น๋์๋ณด๋ ์ ์
- Flutter
- Streamlit๊ธฐ์ด
- db
- java
- ์คํธ๋ฆผ๋ฆฟ
- ํ์ด์ฌ
- Today
- Total
ruriruriya
[React] ๋ฆฌ์กํธ - React Router๋ฅผ ์ฌ์ฉํ์ฌ ํค๋ ํธํฐ ๊ณ ์ ํ๊ธฐ(BrowserRouter, Routes, Route) ๋ณธ๋ฌธ
[React] ๋ฆฌ์กํธ - React Router๋ฅผ ์ฌ์ฉํ์ฌ ํค๋ ํธํฐ ๊ณ ์ ํ๊ธฐ(BrowserRouter, Routes, Route)
๋ฃจ๋ฆฌ์ผใ 2024. 5. 20. 23:07
BrowserRouter, Routes, Route๋ react-router-dom ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ฃผ์ ์ปดํฌ๋ํธ๋ก, React ์ ํ๋ฆฌ์ผ์ด์
์์ ํด๋ผ์ด์ธํธ ์ฌ์ด๋ ๋ผ์ฐํ
์ ์ฝ๊ฒ ๊ตฌํํ ์ ์๋๋ก ๋์์ค๋ค.
์ฝ๊ณ ์ฌํํ ์ฝ๋๋ก ํค๋์ ํธํฐ๋ฅผ ๊ณ ์ ํด๋ณด์.
1. BrowserRouter, Routes, Route
1-1. BrowserRouter
BrowserRouter๋ HTML5์ History API๋ฅผ ์ฌ์ฉํ์ฌ UI๋ฅผ ์
๋ฐ์ดํธํ๊ณ URL์ ๋๊ธฐํํ๋ ๋ผ์ฐํฐ๋ก
์ ํ๋ฆฌ์ผ์ด์
์ ์ต์์ ์ปดํฌ๋ํธ๋ก ๊ฐ์ธ์ ธ์ผ ํ๋ฉฐ,
์ด๋ฅผ ํตํด ํ์ ์ปดํฌ๋ํธ๋ค์ด ๋ผ์ฐํ
๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.
import { BrowserRouter as Router } from 'react-router-dom';
const App = () => (
<Router>
{/* ๋ผ์ฐํ
๊ด๋ จ ์ปดํฌ๋ํธ๋ค */}
</Router>
);
1-2. Routes
Routes๋ ๋ผ์ฐํธ ๋งค์นญ์ ๊ด๋ฆฌํ๋ ์ปดํฌ๋ํธ์ด๋ค..
์ฌ๋ฌ ๊ฐ์ Route ์ปดํฌ๋ํธ๋ฅผ ์์์ผ๋ก ๊ฐ์ง๋ฉฐ,
ํ์ฌ URL๊ณผ ์ผ์นํ๋ Route๋ฅผ ์ฐพ์ ํด๋น ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ๋ค.
import { Routes, Route } from 'react-router-dom';
const App = () => (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Router>
);
1-3. Route
Route๋ ํน์ ๊ฒฝ๋ก(path)์ ํด๋น ๊ฒฝ๋ก์ ๋งค์นญ๋๋ ์ปดํฌ๋ํธ(element)๋ฅผ ์ ์ํ๋ ์ปดํฌ๋ํธ์ด๋ค.
URL์ด ์ง์ ๋ ๊ฒฝ๋ก์ ์ผ์นํ๋ฉด ํด๋น ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง๋๋ค.
- path: ๋ผ์ฐํธ ๊ฒฝ๋ก๋ฅผ ์ง์ ํ๋ค. ์๋ฅผ ๋ค์ด, /๋ ๋ฃจํธ ๊ฒฝ๋ก, /about์ About ํ์ด์ง ๊ฒฝ๋ก
- element: ๊ฒฝ๋ก๊ฐ ๋งค์นญ๋ ๋ ๋ ๋๋งํ ์ปดํฌ๋ํธ๋ฅผ ์ง์
import { Route } from 'react-router-dom';
const App = () => (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Router>
);
2. Router๋ก ํค๋ ํธํฐ ๊ณ ์ ํ๊ธฐ
2-1. ํจํค์ง ์ค์น
npm install react-router-dom
2-2. Header, Footer ์ปดํฌ๋ํธ ์์ฑ
// Header.js
import React from 'react';
import './Header.css';
const Header = () => {
return (
<header className="header">
<h1>Header</h1>
</header>
);
};
export default Header;
// Footer.js
import React from 'react';
import './Footer.css';
const Footer = () => {
return (
<footer className="footer">
<p>Footer</p>
</footer>
);
};
export default Footer;
2-3. ๊ฐ ํ์ด์ง ์ปดํฌ๋ํธ ์์ฑ
ํค๋์ ํธํฐ ์ฌ์ด์ ์ฝํ ์ธ ํ์ด์ง๋ฅผ ์ฌ๋ฌ๊ฐ ์์ฑํ๋ค.
// Home.js
import React from 'react';
const Home = () => {
return (
<div>
<h2>Home Page</h2>
<p>Welcome to the home page!</p>
</div>
);
};
export default Home;
// About.js
import React from 'react';
const About = () => {
return (
<div>
<h2>About Page</h2>
<p>This is the about page.</p>
</div>
);
};
export default About;
2-4. App ์ปดํฌ๋ํธ์ BrowserRouter ์ถ๊ฐ
react-router-dom์ BrowserRouter, Routes, Route๋ฅผ ์ฌ์ฉํ์ฌ ๋ผ์ฐํ ์ ์ค์ .
// App.js
import React from 'react';
import { Routes, Route } from 'react-router-dom';
import Header from './layout/Header';
import Footer from './layout/Footer';
import Home from './pages/Home';
import About from './pages/About';
import './App.css';
const App = () => {
return (
<div className="app">
<Header />
<main className="content">
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</main>
<Footer />
</div>
);
};
export default App;
2-5. index.js์ BrowserRouter ์ถ๊ฐ
import React from 'react';
import ReactDOM from 'react-dom/client';
import {BrowserRouter} from 'react-router-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</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();
2-6. ๊ฐ ์ปดํฌ๋ํธ์ css๋ฅผ ์ค์ ํ์ฌ ํค๋์ ํธํฐ๋ฅผ ๊ณ ์
/* Header.css */
.header {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 60px;
background-color: #333;
color: white;
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
}
/* Footer.css */
.footer {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
height: 40px;
background-color: #333;
color: white;
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
}
/* App.css */
.app {
padding-top: 60px; /* Header ๋์ด๋งํผ ํจ๋ฉ ์ถ๊ฐ */
padding-bottom: 40px; /* Footer ๋์ด๋งํผ ํจ๋ฉ ์ถ๊ฐ */
}
.content {
padding: 20px;
}
3. ์คํํ๋ฉด
3-1. Home ๋ฉ๋ด ํด๋ฆญ ์
3-2. About ๋ฉ๋ด ํด๋ฆญ ์
'๐๏ธWeb > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] ๋ฆฌ์กํธ - React์์ ๋ค๋น๊ฒ์ด์ ๋ฐ ๋ง๋ค๊ธฐ(react-router ๋ผ์ด๋ธ๋ฌ๋ฆฌ) (0) | 2024.05.07 |
---|---|
[React] ๋ฆฌ์กํธ - props ์ฌ์ฉ ๋ฐฉ๋ฒ (0) | 2024.04.29 |
[React] ๋ฆฌ์กํธ - ํจ์ ์ปดํฌ๋ํธ ์ ์์ ์ฌ์ฉ (0) | 2024.04.27 |
[React] ๋ฆฌ์กํธ - ๋น๋ ๋ฐ ๋ฐฐํฌ (0) | 2024.04.27 |
[React] ๋ฆฌ์กํธ - React๊ธฐ๋ณธ ๊ตฌ์กฐ ๋ฐ ํ์ผ ์ญํ (0) | 2024.04.27 |