Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I do dispatch from getters in Vuex

Fiddle : here

I am creating a webapp with Vue 2 with Vuex. I have a store, where I want to fetch state data from a getter, What I want is if getter finds out data is not yet populated, it calls dispatch and fetches the data.

Following is my Vuex store:

const state = {   pets: [] };  const mutations = {   SET_PETS (state, response) {     state.pets = response;   } };  const actions = {  FETCH_PETS: (state) => {       setTimeout(function() {              state.commit('SET_PETS', ['t7m12qbvb/apple_9', '6pat9znxz/1448127928_kiwi'])     }, 1000)  } }  const getters = {     pets(state){     if(!state.pets.length){         state.dispatch("FETCH_PETS")     }     return state.pets   } }  const store = new Vuex.Store({   state,   mutations,   actions,   getters }); 

But I am getting following error:

Uncaught TypeError: state.dispatch is not a function(…)

I know I can do this, from beforeMount of Vue component, but I have multiple components which uses same Vuex store, so I have to do it in one of the components, which one should that be and how will it impact other components.

like image 875
Saurabh Avatar asked Nov 18 '16 13:11

Saurabh


People also ask

Can getters Call actions Vuex?

commit to commit a mutation, or access the state and getters via context. state and context. getters . We can even call other actions with context.

How do I dispatch an action on Vuex?

You can call another Vuex action by passing the name of that action as a string as the first argument of dispatch : const store = new Vuex. Store({ actions: { walk(context) { context. dispatch("goForward"); }, goForward(context) { // }, }, });

Should I use getters in Vuex?

Vuex getters is to Vue computed as Vuex state is to Vue data. In an action or getter, if you use a list of products multiple times to find a result would you then use getters.

What is difference between dispatch and commit in Vuex?

Dispatch triggers an action whereas commit triggers a mutation. $dispatch is always used from your methods in routes/components. It sends a message to Vuex store to perform some action. The action can be performed any time after the current tick so that it will not affect the frontend performance.


2 Answers

Getters can not call dispatch as they are passed the state not context of the store

Actions can call state, dispatch, commit as they are passed the context.

Getters are used to manage a 'derived state'.

If you instead set up the pets state on the components that require it then you would just call FETCH_PETS from the root of your app and remove the need for the getter

like image 82
GuyC Avatar answered Sep 21 '22 13:09

GuyC


I know this is an older post and I'm not sure if this is good practice, but I did the following to dispatch from a getter in my store module:

import store from "../index"

And used the store inside my getter like this:

store.dispatch("moduleName/actionName")

I did this to make sure data was made available if it was not already present.

*edit: I want you to be aware of this: Vue form - getters and side effects

This is related to @storsoc note.

If you need to dispatch from your getter you probably are already implementing your state wrong. Maybe a component higher up should already have fetched the data before (state lifting). Also please be aware that getters should only be used when you need to derive other data from the current state before serving it to your template otherwise you could call state directly: this.$store.state.variable to use in methods/computed properties.

Also thing about your lifecycle methods.. you could for example in your mounted or created methods check if state is set and otherwise dispatch from there. If your getter / "direct state" is inside a computed property it should be able to detect changes.

like image 38
Jeffrey van der Lem - Meesters Avatar answered Sep 18 '22 13:09

Jeffrey van der Lem - Meesters