Currently, I made a simple cart in Vue with Vuex. The issue I'm having is that I'm updating the quantity of a product inside a cart object, but this does not update on the front-end view (it's not reactive).
What I've current done is updated an object value inside the cart array, for it to work I needed to set the existing cart to an empty array, then I re-applied the old cart with the new value to the cart state.
What would be the best way to update a value of an object inside an array?
Here are the following relevant data from my module (vuex):
namespaced: true,
state: {
cart: [],
products: [
{
id: 2,
name: 'Product sweater 2',
},
{
id: 1,
name: 'Product sweater',
}
]
},
mutations: {
update(state, product) {
const index = state.cart.findIndex(x => x.id === product.id)
if (index !== -1) {
state.cart[index].quantity++
state.cart = [
...state.cart
]
} else {
product.quantity = 1
state.cart.push(product)
}
},
}