Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuex state is null in mutation after page refresh instead predefined object

In a simple application, I create a simple set of mutations and actions that are tied to the component creation hook. In browser, after pressing F5, when vue-devtools opened on Vuex tab I get an error at the start of the application, although this should not happen.

Main question: why state is 'null' and how to change it?

store.js

export default new Vuex.Store({
  state: {
    a: undefined,
    b: undefined
  },
  mutations: {
    SET_A (state, a) {
      console.info(a)
      state.a = a // ← store.js?c0d6:14
    },
    SET_B (state, b) {
      state.b = b
    }
  },
  actions: {
    setA ({ commit }, a) {
      console.info(a)
      commit('SET_A', a)
    },
    setB ({ commit }, b) {
      commit('SET_B', b)
    }
  }
})

Home.vue

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'home',
  components: {
    HelloWorld
  },
  created: function () {
    this.$store.dispatch('setA', 'A')
  }
}
</script>

About.vue

<script>
export default {
  name: 'home',
  components: {
  },
  created: function () {
    this.$store.dispatch('setB', 'B')
  }
}
</script>

Console log on Components tab

log.js?1afd:24 [HMR] Waiting for update signal from WDS...
store.js?c0d6:22 A
store.js?c0d6:13 A
backend.js:1585  vue-devtools  Detected Vue v2.6.10 

Console log on Vuex tab

log.js?1afd:24 [HMR] Waiting for update signal from WDS...
store.js?c0d6:22 A
store.js?c0d6:13 A
backend.js:1585  vue-devtools  Detected Vue v2.6.10 

store.js?c0d6:13 null

backend.js:14674 Uncaught TypeError: Cannot set property 'a' of null
    at Store.SET_A (store.js?c0d6:14)
    at wrappedMutationHandler (vuex.esm.js?2ba1:725)
    at backend.js:14664
    at Array.forEach (<anonymous>)
    at VuexBackend.replayMutations (backend.js:14664)
    at VuexBackend.onInspectState (backend.js:14355)
    at Bridge.emit (backend.js:5472)
    at Bridge._emit (backend.js:5172)
    at backend.js:5097
    at Array.forEach (<anonymous>)
SET_A @ store.js?c0d6:14
wrappedMutationHandler @ vuex.esm.js?2ba1:725
(anonymous) @ backend.js:14664
replayMutations @ backend.js:14664
onInspectState @ backend.js:14355
emit @ backend.js:5472
_emit @ backend.js:5172
(anonymous) @ backend.js:5097
(anonymous) @ backend.js:5097
listener @ backend.js:2568
postMessage (async)
o @ proxy.js:1

store.js?c0d6:13 null

backend.js:14674 Uncaught TypeError: Cannot set property 'a' of null
    at Store.SET_A (store.js?c0d6:14)
    at wrappedMutationHandler (vuex.esm.js?2ba1:725)
    at backend.js:14664
    at Array.forEach (<anonymous>)
    at VuexBackend.replayMutations (backend.js:14664)
    at VuexBackend.onInspectState (backend.js:14355)
    at Bridge.emit (backend.js:5472)
    at Bridge._emit (backend.js:5172)
    at backend.js:5097
    at Array.forEach (<anonymous>)
SET_A @ store.js?c0d6:14
wrappedMutationHandler @ vuex.esm.js?2ba1:725
(anonymous) @ backend.js:14664
replayMutations @ backend.js:14664
onInspectState @ backend.js:14355
emit @ backend.js:5472
_emit @ backend.js:5172
(anonymous) @ backend.js:5097
(anonymous) @ backend.js:5097
listener @ backend.js:2568
postMessage (async)
o @ proxy.js:1

like image 329
Zakhar Skorokhodov Avatar asked May 24 '19 11:05

Zakhar Skorokhodov


People also ask

Does Vuex state persist on refresh?

To persist Vuex state on page refresh, we can use the vuex-persistedstate package. import { Store } from "vuex"; import createPersistedState from "vuex-persistedstate"; import * as Cookies from "js-cookie"; const store = new Store({ // ...

How do I keep Vuex state from refreshing?

To keep Vuex state on page refresh with Vue. js, we use the vuex-persistedstate package. import Vue from "vue"; import Vuex from "vuex"; import createPersistedState from "vuex-persistedstate"; Vue. use(Vuex); export default new Vuex.

Are Vuex mutations synchronous?

In Vuex, mutations are synchronous transactions: store. commit('increment') // any state change that the "increment" mutation may cause // should be done at this moment.

How do you mutate Vuex state?

Vuex stores are reactive. When Vue components retrieve state from it, they will reactively and efficiently update if the store's state changes. You cannot directly mutate the store's state. The only way to change a store's state is by explicitly committing mutations.


1 Answers

It was "New Vuex backend" in devtools settings.

I don`t know what is the catch, but disable this option solves my problem.

like image 175
Zakhar Skorokhodov Avatar answered Sep 20 '22 19:09

Zakhar Skorokhodov