티스토리 뷰

dev_공부일지/React

파라미터 받기

dev_0hoon 2024. 2. 26. 19:03
        <Route path="/detail/:id" element={
          <Detail shoes={shoes}></Detail>
        } />

이렇게 url 중에 :id를 넣으면

 

import { useParams } from "react-router-dom";

function Detail(props){

  let {id} = useParams();

  console.log(id);

    return (
      <div className="container">
        <div className="row">
          <div className="col-md-6">
            <img src="https://codingapple1.github.io/shop/shoes1.jpg" width="100%" />
          </div>
          <div className="col-md-6">
            <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>
        </div>
      </div> 
    );
  }

  export default Detail;

useParam();을 이용해서 받을 수 있다.

 

'dev_공부일지 > React' 카테고리의 다른 글

Lifecycle  (0) 2024.02.26
styled-component  (0) 2024.02.26
리액트 페이지마다 url 나누기 +404만들기  (1) 2024.02.26
import,export  (0) 2024.02.23
input 다루기  (0) 2024.02.23
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
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
글 보관함