Redux Reducer不会返回整个状态

时间:2020-07-21 06:42:00

标签: reactjs redux reducers

我是 redux 的新手,我的问题是当我调度动作时,它会转到化简器,但状态不会更新。

这是我的初始状态

   const initialState ={
    cart :[ ],
    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 UPDATE_QUANTITY:
              
                return{
                    ...state,
                    cart:state.cart.map((product,i) =>
                    {
                        if(i === (action.product.id-1))
                       {
                           return{product.quantity = action.qty.toString()}
                         

                       }
                           return product
                       
                        
                    })
                } 

其他reducer正常工作并返回正确的状态,但是当我调度此操作时,我的状态未更新。如果对代码有任何改进,请指定

1 个答案:

答案 0 :(得分:0)

您需要从地图返回:

case UPDATE_QUANTITY:
  return {
    ...state,
    cart: state.cart.map((product) => {
      if (product.id === action.product.id) {
        return {...product, quantity: action.qty.toString()}
      }
      return product;
    })
  }