Vuex: Call getters from action

JavascriptEcmascript 6Vuejs2Vuex

Javascript Problem Overview


Is there a way for a dispatch/action to call a getter inside of it?

mutations: {
    setData(state, data) {
        state.data = data;
    }
}
actions: {
    sendDataToServer({ commit }, payload) {
        // call getter (data) and assign to variable
        // do async functions from the data returned
    }
},
getters: {
    getAppData: state => () => {
        return state.data;
    }
}

So what's the best practice here? Using the mutation to change the state and then get the state and pass it to action which will then execute the async function or do I need to restructure my implementation?

call mutation -> get the data via getter -> call action

OR

do it all on the action (do mutation on the action and do the action/async method without the need of the getter)?

Javascript Solutions


Solution 1 - Javascript

In addition to commit, actions has default injected parameters which are dispatch, getters and rootGetters. So you can simply write;

sendDataToServer({ commit, getters }, payload) to access getters.

Solution 2 - Javascript

You have access to getters inside an action:

getters: {
   getUser(state){
      return state.user
   }
}

actions : {
    myAction({ getters }){
       let user = getters.getUser
    }
}

Solution 3 - Javascript

In the action, you see the first parameter has {commit} in it. Similarly, you can pass {commit, state}. This way, you can directly access the state.data.

I think in your example, you would want to do the action because you can call the mutation from inside action itself using commit('setData').

The first parameter is there for you to use state and mutation as you prefer. Personally, I have only worked on projects where you do the action first and do mutation to store it in the app. For example, if I want to store a car info in the server somewhere, first I would do the action (and save it to remote db). Once I confirm that it saved in db, I would locally mutate in the store. This totally depends on case by case basis. But good thing is that you can mutate from inside the action

Solution 4 - Javascript

Action handlers receive a context object which exposes the same set of methods/properties on the store instance, so you can call context.commit to commit a mutation, or access the state and getters via context.state and context.getters

   actions: {
            sendDataToServer(context, payload) {
                // context object contains state, commit, getters
                context.getters.getAppData
            }
        },

Refer docs: https://vuex.vuejs.org/guide/actions.html#dispatching-actions

Solution 5 - Javascript

If you are using nuxt and isolated files in vuex, like this =

store -
      |
      |-- index.js
      |
      |-- store.js
      |
      |-- product.js

// store.js
export const getters = {
  getNameStore: state => state.getNameStore ? state.getNameStore : null
};

I want the getNameStore of the store.js into product.js

// product.js
export const actions = {
  setResultSearch({ commit, dispatch }, text) {
    console.log(
      'getNameStore',
      this.getters["store/getNameStore"]
  );
};

> this.getters["store/getNameStore"]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThe BassmanView Question on Stackoverflow
Solution 1 - JavascriptTugay İlikView Answer on Stackoverflow
Solution 2 - JavascriptAhmad MobarakiView Answer on Stackoverflow
Solution 3 - JavascriptSujil MaharjanView Answer on Stackoverflow
Solution 4 - JavascriptHarshith VAView Answer on Stackoverflow
Solution 5 - JavascriptMm.Mirzaei.devView Answer on Stackoverflow