When i click this button:
const addToCart = () => {
dispatch({
type: "ADD_TO_CART",
item:{
id:id,
name: name,
image: image,
price: price,
score: score,
releaseDate: releaseDate,
amount: amount // this is defaulted as 1
}
})
}
It sends it to my reducer class that stores data, im trying to edit it so it will only have a single instance of a specific item and only increase the amount
case 'ADD_TO_CART':
for (let x of state.cart){
// this should get fired if the object is found and increase amount by specified
if (x.id === action.item.id){
let newCart1 = [...state.cart];
newCart1[x] = { ...newCart1[x], amount: x.amount += action.item.amount }
return {...state, cart: [newCart1]}
}
}
return { ...state, cart: [...state.cart, action.item], };
// this should get fired if it is not found and add the whole object
There are two issues.
If I add an initial item it works (initial object) if I add a second of the same item it will increase amount to 3 instead of 2
On top of that it adds a "Object object" with amount 3 which I don't know what this is or how it is included given the array is of length 1 (second object)
