问题
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