首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在中更新两次相同的状态

在中更新两次相同的状态
EN

Stack Overflow用户
提问于 2020-07-21 12:31:45
回答 1查看 258关注 0票数 0

如何在同一个减速器中两次更新我的状态

我在redux商店的状态

代码语言:javascript
复制
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
     }
   ],

用于更新状态的

减速器

代码语言:javascript
复制
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的数量。

EN

回答 1

Stack Overflow用户

发布于 2020-07-21 14:15:59

不要发出警告,请尝试:

代码语言:javascript
复制
const updatedCart = {...cart}
updatedCart.find(i => i.id === action.product.id).quantity +=1
return {
      ...state,
      cart: updatedCart
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63014626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档