[Vuejs]-Vuex state not updating data

0👍

Use mutations to only set data. and other things do on action. like:

Action:

const actions = {
   async fetchMenuData({ commit }) {
      const response = await axios.get('../api/Menu/GetMenu');
      let userName = response.data.userName;
      commit('setUserName', userName);
   }
}

And mutations:

const mutations = {
    setUserName(state, userName) {        
       state.userName = userName;
    }
}

Dont forget to dispatch the function fetchMenuData

Properly not sure, why this happens. But, I faced this problem and solved by this way.

0👍

 axios.get('../api/Menu/GetMenu')
  .then(({ data }) => {
    commit('setUserName', data.userName);
  }).catch(error => {    })

It is better to make a commit in then()

Leave a comment