0

My Problem is, that the state-variable "genreRankings" in "store.js" is never updating. Can somebody tell me why?

I'm accessing the Store via my Component as follows:

saveMovie (item) {
  this.$store.dispatch('addMovie', item).then(x => {
    console.log(this.$store.state.savedMovies)
    this.$store.commit('update_genreRankings', Util.getGenreRankings(this.$store.getters.savedMovies))
  })
},

removeMovie (item) {
  this.$store.dispatch('removeMovie', item).then(x => {
    this.$store.commit('update_genreRankings', Util.getGenreRankings(this.$store.getters.savedMovies))
  })
},

Here is store.js (https://gist.github.com/oaltena/ccc70c06c29a1d9af6aa3234aba79518) and Util.js (https://gist.github.com/oaltena/67b8431199e9a6d74681c04d9183e630).

When i access the "genreRankings" via VueDevTools the array is always empty.

Help, please! :-)

2
  • Additional Info: "savedMovies" is not empty but filled with test data from the localstorage. Commented Nov 3, 2018 at 15:43
  • Had any luck solving this problem? I'm having a similar issue. Commented Nov 16, 2018 at 18:03

2 Answers 2

2

Try "replacing" the state with a new array :

 state.savedMovies = state.savedMovies.concat(object)

As written in the Vuex documentation, the state of Vuex store follows the same rules as the state in the components : https://vuex.vuejs.org/guide/mutations.html#mutations-follow-vue-s-reactivity-rules

PS: it's pretty ugly to call mutations directly from the components, use mapActions to map your actions in your components, then call commit from the action. You'll make a more maintenable code.

Sign up to request clarification or add additional context in comments.

Comments

0

Try replacing this:

update_genreRankings (state, object) {
  state.genreRankings = object
}

with this:

update_genreRankings (state, object) {
  Vue.set(state, 'genreRankings', object)
}

Reference: https://v2.vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.