Unable to delete or increase cart count

筅森魡賤 提交于 2020-08-10 18:53:10

问题


On a cart page, there are two icons plus and minus to increase and decrease the count respectively. when I use to increase the count, it appends a new array into the array instead of an object. The increase code is

case 'ADD_TO_CART':
       {
           const updatedCart = [...state.cart];
           const item = updatedCart.find(x=>x.key===action.payload.key);
           if(item)
           {
            item.count++;
           }
           else{
             updatedCart.push(action.payload);
           }
            return {
                ...state,
                cart: updatedCart,
                total: state.total + 1
            }
       }

For decrease, the code is

case 'REMOVE_FROM_CART' :
            let updatedCart = [...state.cart];
            const item = updatedCart.find(x=>x.key===action.payload.key);
            if (item && action.payload.count > 0)
            {
                action.payload.count--
            } else {
               updatedCart = state.cart.filter(i => i.key !== action.payload.key) 
            }
            return {...state, cart: updatedCart, total : state.total - 1 }

Map Dispatch to props code

const mapDispatchToProps = (dispatch) =>{
  return {
    removeItem:(cart) =>dispatch({type:'REMOVE_FROM_CART',payload:cart}),
    addItemsToCart:(cart) =>dispatch({type:'ADD_TO_CART',payload:cart})
  }
} 

onPress Function calling

<AntDesign name="pluscircleo" size={30} color="black" onPress={() =>this.props.removeItem(this.props.cartItems.cart)}

来源:https://stackoverflow.com/questions/62996065/unable-to-delete-or-increase-cart-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!