I have several vuex
modules. For simplicity, I'll explain my problem with an example of what I am facing:
I have 2 vuex
modules firstModule.js and secondModule.js
firstModule.js has a myArray[ ]
in its state:
//firstModule
const state = {
myArray: [//has some items]
}
In secondModule.js I have an action which fetches some data asynchronously from an external API.
This action commits a mutation which should search whether the fetched data is present in firstModule's myArray[ ]
if the fetched data is present in firstModule's myArray[ ]
, then add it to secondModule's newArray[ ]
const state = {
newArray: []
}
const mutations = {
addToNewArray: (state, payload) => {
function findIyem(value){
return value === payload.data;
};
var item = payload.rootData.myArray.find(findItem);
state.newArray.push(payload.data);
}
}
const actions = {
fetchData: ({commit, rootState}) => {
// fetches some data from external API
var fetched data = data // data is which I received from fetching
commit('addToAnotherArray', {data: data, rootData: rootState.firstModule.myArray}
}
}
But, here's the problem:
according to docs mutations in modules does not get access to rootState, only actions and getters get the access to rootState
without access to rootAtate in mutations, how can I perform the logic as I performed above?
Do I have to access the rootState in the actions and pass it to the committed mutation as a payload as I have done above?
You can make the check to your firstModule's myArray within the fetchData action. That's the best practice. But, if you really need to access the rootState variable in a mutation, you have to pass it in as a variable reference, like you are currently.
To change another module state from one module in Vuex, we can call commit with the mutation name with the root option set to true . commit("TOGGLE_LOADING", null, { root: true }); to call commit to commit the TOGGLE_LOADING mutation with root set to true .
The rootState gets important if I need to access the general stuff in the index. js or if I want to access data from a module from inside another module. E.g.: To put a booking I need to know which categorie is selected from the current user of my app.
Vuex gives you the flexibility to manage multiple store modules based on how you'd like to structure your project.
Sorry, something went wrong. IMO, mutations should not have rootState. An action is the intent of a mutation, and the mutation is the finalisation of that intent. Therefore with that reasoning. Actions should be used to collate the data to commit to that the mutation.
The way we can trigger a real mutation from a module is by using an action. If we go to the todos module, we had defined an action there to add that todo asynchronously, and here we commit an app todo mutation. From here, if we write commit sum, we would be triggering a todos/sum mutation because this module is in that space.
But sometimes you need to call something from the outside. This lesson shows how you can call an external Mutation and Action from within a module in TypeScript. Aside from modules, we can also have our rootState mutations, actions, and getters.
The "workaround" (I would say: intended usage) is to repare the data in the action, which does have access to rootState I'm also confused why you expect an update on a closed issue. Sorry, something went wrong. Consider this. An action is used to fetch data.
In my experience, mutations in a vuex
module should directly affect the model in the way they describe. So, your addToNewArray
method should simply add the payload data to the state's newArray
property.
Actions are where your logic code should run to determine whether or not to make mutations. You can make the check to your firstModule's myArray
within the fetchData
action. That's the best practice.
But, if you really need to access the rootState
variable in a mutation, you have to pass it in as a variable reference, like you are currently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With