question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Correct way to call a mutation within an action

See original GitHub issue

Hey everyone,

I have a short question, if this is a correct way to trigger a mutation within an action:

export class MyModule extends VuexModule {
   myProp: string = '';

   @Mutation
   private SET_MY_PROP(value: string) {
       this.myProp = value;
   }

   @Action
   public async setMyProp(value: string) {
      this.SET_MY_PROP(value);
   }
}

I am asking, because those get started examples commit the mutation differently like:

   @Action({ commit: 'SET_MY_PROP' })
   async setMyProp(value: string) {
      return value;
   }

or like

   @Action
   async setMyProp(value: string) {
      this.context.commit('SET_MY_PROP', value);
   }

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:5

github_iconTop GitHub Comments

6reactions
EtienneBruinescommented, Aug 3, 2020

@souphuhn Personally I use the first example, calling the method instead of annotating. Reasons:

  • tooling recognizing that SET_MY_PROP is actually used (and therefore not dead code)
  • code-completion works
  • intuitive for newcomers to your project (it’s just calling a class method)

One ‘danger’ I have noticed, is that it’s tempting to use more than one argument for the mutation – your IDE won’t mind, but it won’t work as expected.

// This won't work
export class MyModule extends VuexModule {
   myProp: string = '';

   @Mutation
   private SET_MY_PROP(value1: string, value2: string) {
       this.myProp = value1 + value2;
   }

   @Action
   public async setMyProp(value: string) {
      this.SET_MY_PROP('hello', value);
   }
}
// This will
export class MyModule extends VuexModule {
   myProp: string = '';

   @Mutation
   private SET_MY_PROP([value1, value2]: [string, string]) {
       this.myProp = value1 + value2;
   }

   @Action
   public async setMyProp(value: string) {
      this.SET_MY_PROP(['hello', value]);
   }
}
0reactions
Pacheco95commented, Apr 18, 2021

@bdockbockd just call your mutations from your actions like normal method calls

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vuex showdown: Mutations vs. actions - LogRocket Blog
Actions are similar to mutations, the differences being that: Instead of mutating the state, actions commit mutations; Actions can contain ...
Read more >
Please add an ability to call mutation from another ... - GitHub
If you call a mutations in other mutations, they cannot be separated in devtools and it decrease trackability.
Read more >
Mutations | Vuex
The only way to actually change state in a Vuex store is by committing a mutation. Vuex mutations are very similar to events:...
Read more >
Call a Root Vuex Mutation or Action from within a Module in ...
The way we can trigger a real mutation from a module is by using an action. If we go to the todos module,...
Read more >
What are mutations in Vuex? - Educative.io
In the below code, we can see the state and mutation object in the store. The changeStatus mutation is used to update the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found