【发布时间】:2020-11-10 20:10:00
【问题描述】:
如何在同一个 reducer 中两次更新我的状态
我在 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)
}
}
这里我想将 product 更新为 cart state 并且我想增加同时更新到cart 的数量
【问题讨论】: