My state in vuex store is huge.
Is there a way to reset all the data in state in one go, instead of manually setting everything to null?
My state in vuex store is huge.
Is there a way to reset all the data in state in one go, instead of manually setting everything to null?
If you do a state = {}, you will remove the reactivity of the properties and your getters mutations will suddenly stop working.
you can have a sub-property like:
Doing a state.subProperty = {} should help, without losing the reactivity.
You should not have a state too big, break them down to different modules and import to your vuex store like so:
now in your individual files:
If you should want to clear the state you can just have a mutation implement:
Update after using the below solution a bit more
So it turns out that if you use
replaceState
with an empty object ({}
) you end up bricking reactivity since your state props go away. So in essence you have to actually reset every property in state and then usestore.replaceState(resetStateObject)
. For store without modules you'd essentially do something like:Update (from comments): What if one needs to only reset/define a single module and keep the rest as they were?
If you don't want to reset all your modules, you can just reset the modules you need and leave the other reset in their current state.
For example, say you have mutliple modules and you only want to reset module
a
to it's initial state, using the method above^, which we'll callresetStateA
. Then you would clone the original state (that includes all the modules before resetting).where
deepClone
is your deep cloning method of choice (lodash has a good one). This clone has the current state of A before the reset. So let's overwrite thatand use that new state with
replaceState
, which includes the current state of all you modules, except the modulea
with its initial state:Original solution
I found this handy method in
Vuex.store
. You can clear all state quickly and painlessly by usingreplaceState
, like this:It works with a single store or with modules, and it preserves the reactivity of all your state properties. See the Vuex api doc page, and find in page for
replaceState
.For Modules
IF you're replacing a store with modules you'll have to include empty state objects for each module. So, for example, if you have modules
a
andb
, you'd do:I am not sure what you use case is, but I had to do something similar. When a user logs out, I want to clear the entire state of the app - so I just did
window.reload
. Maybe not exactly what you asked for, but if this is why you want to clear the store, maybe an alternative.I have just found the great solution that works for me.
Reset Vuex Module State Like a Pro
Thanks to Taha Shashtari for the great solution.
Michael,
Based on these 2 answers (#1 #2) I made a workable code.
My structure of Vuex's
index.js
:Inside each module we need to move all states into separated var
initialState
and in mutation define a functionresetState
, like below formedia.js
:In Vue component we can use it like:
You can declare an initial state and reset it to that state property by property. You can't just do state = initialState or you lose reactivity.
Here's how we do it in the application I'm working on: