Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access rootState in mutations of different modules

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?

like image 237
Vamsi Krishna Avatar asked Apr 19 '17 11:04

Vamsi Krishna


People also ask

How do I access rootState in mutation?

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.

How do you commit a mutation from another module Vuex?

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 .

What is rootState?

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.

Can you have multiple Vuex stores?

Vuex gives you the flexibility to manage multiple store modules based on how you'd like to structure your project.

Should a mutation have a rootstate?

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.

How do you trigger a mutation from a module?

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.

Can you call an external mutation and action from within a module?

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.

What is the workaround for action with access to rootstate?

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.


1 Answers

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.

like image 106
thanksd Avatar answered Sep 22 '22 10:09

thanksd