<Card **shoes={shoes}**></Card>
function Card(**props**) {
return (
<div className="col-md-4">
<img src="https://codingapple1.github.io/shop/shoes1.jpg" width="80%"/>
<h4>{**props**.shoes[0].title}</h4>
<p>{**props**.shoes[0].price}</p>
</div>
)
}
function Card(props) {
return (
<div className="col-md-4">
<img src="https://codingapple1.github.io/shop/shoes1.jpg" width="80%"/>
<h5>**{props.shoes.title}**</h5>
<p>**{props.shoes.price}**</p>
</div>
)
}
<Card shoes={shoes**[0]**}></Card>
<Card shoes={shoes**[1]**}></Card>
<Card shoes={shoes**[2]**}></Card>
→ shoes라는 state변수를 각각 shoes[0]. shoes[1], shoes[2] 로 저장을 해놨으므로 위의 Card에서 {props.shoes.title}, {props.shoes.price} 만 써도 됨! 인덱싱 안하고!!!
→ 이미지는 똑같은 경로를 가지고 있기 때문에
<Card shoes={shoes[0]} **i={1}**></Card>
<Card shoes={shoes[1]} **i={2}**></Card>
<Card shoes={shoes[2]} **i={3}**></Card>
{'문자열' **+ props.i +** '문자열'}
ex) props.i라는 변수를 넣는 문법을 사용했음
#아래 이 코드를
<img src="https://codingapple1.github.io/shop/shoes1.jpg" width="80%"/>
#이렇게 바꾸자
<img src={'https://codingapple1.github.io/shop/shoes' **+ props.i +** '.jpg'} width="80%"/>
{'문자열' + 변수 + '문자열'}
→ 중괄호로 감싸고, +문법을 사용하자
<img src={'https://codingapple1.github.io/shop/shoes' + props.i + '.jpg'} width="80%"/>
Q. shoes 갯수만큼 생성해주세요~
<Card shoes={shoes[0]} i={1}></Card>
<Card shoes={shoes[1]} i={2}></Card>
<Card shoes={shoes[2]} i={3}></Card>
{
**shoes**.map(**(a,i)**=>{
return (
**<Card shoes={shoes[i]} i={i}></Card>**
)
})
}
→ shoes state의 갯수만큼 반복
→ i는 0부터 1씩 증가하는 파라미터
<img src={'https://codingapple1.github.io/shop/shoes' + **(props.i+1)** + '.jpg'} width="80%"/>
→ shoes indexing은 0부터 시작하고, i는 원래 0부터 시작하는데, 1부터 시작해야 하기 때문에 props.i+1을 해줌
우리가 지정한 i는 1,2,3 이 순서대로 증가하는데 파라미터 i 는 0부터 1씩 증가함.
{
shoes.map((a,i)=>{
return (
<Card shoes={shoes[i]} **i={i}**></Card>
)
})
}
<img src={'https://codingapple1.github.io/shop/shoes' + **(props.i+1)** + '.jpg'} width="80%"/>
{
shoes.map((a,i)=>{
return (
<Card shoes={shoes[i]} **i={i+1}**></Card>
)
})
}
<img src={'https://codingapple1.github.io/shop/shoes' + **props.i** + '.jpg'} width="80%"/>