๋ฆฌ์กํธ ๋ผ์ฐํธ ์ ์ผ๋ก ๊ตฌํํด๋ณด์ => useParams()ํจ์ ์ด์ฉ
- App.js
import './App.css';
import { useState } from 'react';
import data from './data.js';
import {Routes, Route } from 'react-router-dom';
import DeatailPage from './pages/DetailPage';
function App() {
let [shoes] = useState(data);
let navigate = useNavigate();
return (
<div className="App">
<Routes>
<Route path="/detail/:id" element={<DeatailPage shoes={shoes}></DeatailPage>}/> //์์ธํ์ด์ง๋ก ์ด๋
</Routes>
</div>
);
}
export default App;
- DetailPage.js
import { useParams } from "react-router-dom";
function DeatailPage(props) {
let {id} = useParams(); //useParamsํจ์๋ฅผ ์ด์ฉํ์ฌ urlํ๋ผ๋ฏธํฐ ๋ฐ์์ค๊ธฐ
return (
<div className="container">
<h4 className="pt-5">{props.shoes[id].title}</h4>
<p>{props.shoes[id].content}</p>
<p>{props.shoes[id].price}</p>
<button className="btn btn-danger">์ฃผ๋ฌธํ๊ธฐ</button>
</div>
)
}
export default DeatailPage;
'๐ปWEB FrontEnd > ํ๋ ์์ํฌ React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Ajax๋ก ์๋ฒ์ ํต์ (0) | 2022.07.08 |
---|---|
์ปดํฌ๋ํธ์ Lifecycle (0) | 2022.07.08 |
๋ผ์ฐํ (์ ํ , navigate, nested routes, outlet) (0) | 2022.07.07 |
import, export (0) | 2022.07.07 |
์ด๋ฏธ์ง ์ฝ์ ํ๋ ๋ฐฉ๋ฒ (0) | 2022.07.07 |