1

I am struggling to understand the concept of mutations in Vuex. All I've found online is that

The only way to actually change state in a Vuex store is by committing a mutation

but I could easily change the state directly within a component, for example like this:

computed: {
  message: {
    get() {
      return this.$store.state.message
    },
    set(newmessage) {
      this.$store.state.message = newmessage
    }
  }
}

(the complete example is here)

Writing mutations imply a lot of boring boilerplate code so why should I use them?

Is there anything wrong about my example?

Thanks!

dariosalvi
  • 405
  • 4
  • 13
  • The whole point of redux-like state management is, well, state management. Your code apparently works fine when you're the only person working on the small project. When team becomes large, things become complicated; that's when architecture becomes handy. And you can't have architecture without supporting blocks - that is, boilerplate. – raina77ow Aug 30 '18 at 10:57
  • thanks for the answer, but allow me to argue that this question is not a duplicate, in fact I had read that question before with care, and couldn't find an answer. What I read is always the same: "state management" and "debug tools". OK but why are mutations better than directly changing the state?Is that because you have a nicer debugging tool than just a console.log() ? – dariosalvi Aug 30 '18 at 11:32
  • I would tend to agree with OP @dariosalvi . It seems that the main answers are always the same but the tool "allows" you to modify the state directly. So what are the tangible benefits of using the mutations (i.e. swallowing the bitter pill of adding plenty of boilerplate to your codebase) ? – 2dvisio Aug 30 '18 at 14:53
  • @raina77ow Are we sure this is a duplicate question? Here there is no discussion about Actions vs. Mutations. – 2dvisio Aug 31 '18 at 09:50

0 Answers0