๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ปWEB FrontEnd/ํ”„๋ ˆ์ž„์›Œํฌ React

detail ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ

๋ฆฌ์•กํŠธ ๋ผ์šฐํŠธ ์ ์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ณด์ž => 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;