1 回答

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超21個(gè)贊
updatevalue您只需在映射購物車時(shí)將索引傳遞給,并更新price該索引處的數(shù)組即可。轉(zhuǎn)換updatevalue為柯里化函數(shù)以使用索引并返回事件處理程序。
const updatevalue = index => () => {
setPrice(prices => prices.map((price, i) => i === index ? price * 2 : price));
}
...
return (
<div className='container'>
{ShoppingCart.map((products, i)=>( // <-- cart index
<div>
<span>
<img style={Style} src={products.image} />
</span>
<span>{price}</span>
<span>{quantity}</span>
<span>
<button onClick={updatevalue(i)}>+</button> // <-- pass index to handler
</span>
</div>
))}
</div>
)
添加回答
舉報(bào)