/detail/:id
🎤
/detail/아무거나 라는뜻
→ /detail/아무거나에 접속하면 이 페이지 보여주세요~
ex) /detail/0으로 접속하면 0번상품
/detail/1로 접속하면 1번상품 나오도록
<Route path="**/detail/:id**" element={<Detail shoes={shoes}/>} />
→ props 활용하면 컴포넌트 하나로 각각 다른 내용 가능
{props.shoes[현재url에입력한숫자].title}
{props.shoes[0].title}
{props.shoes[0].content}
{props.shoes[0].price}
{props.shoes[1].title}
{props.shoes[1].content}
{props.shoes[1].price}
⇒ useParams 사용!!
→ if 조건문으로 해결
→ id라는 변수가 이상하면 상품없다는 UI 보여주자
<Route path="/detail/**:id**" element={<Detail shoes={shoes}/>}/>
→ 유저가 :id 자리에 적은거 가져와줌
let {id} = useParams();
<h4 className="pt-5">{props.shoes[id].title}</h4>
<p>{props.shoes[id].content}</p>
<p>{props.shoes[id].price}원</p>
<Route path="/detail/:id/:hello" element={<Detail shoes={shoes}/>} />
→ /detail/0 접속시 0번째 상품 말고 상품 id가 0인걸 보여주려면
→ 현재 url에 입력한 번호와 같은 번호를 가진 상품을 찾아서 데이터바인딩해주세요
array자료.find(()⇒{return 조건식})
→ 조건식에 맞는 자료를 남겨줌
→ array자료.id == url에 입력한번호 일 경우 결과를 변수에 담아줌
→ {상품1개} 가 남음
let 찾은상품 = props.shoes.find(function(x) {
return x.id == id
});
{찾은상품.title}
let 찾은상품 = props.shoes.find(function(x) {
return x.id == id
});
let 찾은상품 = props.shoes.find**((x)=>x.id == id)**
→ arrow function에서 return과 중괄호는 동시에 생략 가능!!
import { useParams } from "react-router-dom";
function Detail (props) {
let {id} = useParams();
**let 찾은상품 = props.shoes.find(function(x) {
return x.id == 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">{**찾은상품**.title}</h4>
<p>{**찾은상품**.content}</p>
<p>{**찾은상품**.price}원</p>
<button className="btn btn-danger">주문하기</button>
</div>
</div>
</div>
)
}
export default Detail;