[Vuejs]-Vuex pass mutable object as prop, call mutation with the passed object as argument

0👍

Well, there are multiple ways of doing this, but I’ll explain a recommended way of doing.
What you’re doing right now is that, you’re fetching items from store, passing them to a child component (By iteration) then mutating following mutation,

// ...
mutations: {
  changeItemXValue(state, { item, value }) {
    state.items.find((i) => i === item).x = value
  }
}

First of all, you don’t need to mutate this item directly, instead you should create an action that finds the item for you and then mutate it using item id or index.
So what you need to do is that, create an action, when changing value of item, dispatch that action, and pass item id or even item to that action. Inside that action, find the item you want to mutate and then commit mutation, inside your mutation, simply write

state.items[itemIndex] = newValue

Leave a comment