如何在同一个减速器中两次更新我的状态
我在redux商店的状态
const initialState = {
cart:[] as any ,
Products:[
{
id: "1",
title: "Realme 3 pro",
image: {
img1: "https://www.monotain.com/wp-content/uploads/2019/12/realme-5-pro.jpg",
img2: "https://static.techspot.com/images/products/2019/smartphones/org/2019-05-20-product-7.jpg"
},
price: "10000",
details: "Take photography a notch higher with the Realme 3 Pro as it comes with a 25 MP AI Selfie Camera and 16 MP + 5 MP Rear Camera with Sony IMX519 Sensor. The Qualcomm Snapdragon 710 AIE, multi-core AI Engine and Adreno 616 GPU redefine your smartphone experience.",
seller: "Cloudtail India",
quantity: 0
}
],用于更新状态的
减速器
case ADD_TO_CART :
let num = parseInt(action.product.quantity) + 1
console.log(num)
if (state.cart.includes(action.product)) {
alert("Please increase the product at cart page");
} else {
return {
...state,
cart: state.cart.concat(action.product)
}
}在这里,我希望将产品更新为cart状态,同时也希望增加更新到cart的数量。
发布于 2020-07-21 14:15:59
不要发出警告,请尝试:
const updatedCart = {...cart}
updatedCart.find(i => i.id === action.product.id).quantity +=1
return {
...state,
cart: updatedCart
}https://stackoverflow.com/questions/63014626
复制相似问题