Are you supposed to use array methods directly on the computed properties?
rylax opened this issue · 1 comments
I am using vuex-map-fields in my nuxt application and I have encountered an interesting behavior while using array methods in my component.
I have a variable called myArray stored in the vuex store which I am accessing in my component through:
computed: { ...mapFields(["myArray"]) }
While this.myArray.push()
throws an error with the message "You should not directly modify the vuex state" - using this.myArray.splice()
works perfectly. What is the explanation for this and is it suggested to directly modify the array inside a component function or should we create new custom mutations in the store?
No. Don’t use array methods. You must override the field value with this.myArray = value.
I don’t know why push triggers the error and splice not. But that’s a Vuex thing. Both (and all other) array methods mutate the state directly which defeats the point of using Vuex.