在控制台中检查结果很好,但是如果在 setCart 中替换该数组,它不会发生,RecoilJS
const cartState=[
{ id:1, productName:'Apple',price:100,quantity:1}
{ id:2, productName:'Cherry',price:70,quantity:1}
{ id:3, productName:'Orange',price:60,quantity:1}
{ id:4, productName:'Grapes',price:69,quantity:1}
]
const [cart, setCart] = useRecoilState(cartState)
对象是 { id:4, productName:'Grapes',price:69,quantity:1}
const addToCart =(object) => {
if(!cart.includes(object))
{
setCart([...cart, object])
}else
{
let f= cart.map(items=>
{
if(items.id==object.id)
{
return {...items, quantity:items.quantity+ 1}
}
return items
})
setCart(f)
}
}