Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear state in vuex store?

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?

like image 949
KaliCharan Avatar asked Feb 17 '17 10:02

KaliCharan


People also ask

How do I remove a persisted state?

There is no way to delete the persisted state programmatically, there are use cases for removing the state in local storage. If the current behavior is a bug, please provide the steps to reproduce. What is the expected behavior? We should be able to remove the state from local storage either with localStorage.

Does Vuex keep state 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 update my 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.

What is strict mode in Vuex?

In strict mode, whenever Vuex state is mutated outside of mutation handlers, an error will be thrown. This ensures that all state mutations can be explicitly tracked by debugging tools.


2 Answers

I have just found the great solution that works for me.

const getDefaultState = () => {   return {     items: [],     status: 'empty'   } }  // initial state const state = getDefaultState()  const actions = {   resetCartState ({ commit }) {     commit('resetState')   },   addItem ({ state, commit }, item) { /* ... */ } }  const mutations = {   resetState (state) {     // Merge rather than replace so we don't lose observers     // https://github.com/vuejs/vuex/issues/1118     Object.assign(state, getDefaultState())   } }  export default {   state,   getters: {},   actions,   mutations } 

Thanks to Taha Shashtari for the great solution.

Michael,

like image 196
Michael Horojanski Avatar answered Nov 14 '22 06:11

Michael Horojanski


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 use store.replaceState(resetStateObject). For store without modules you'd essentially do something like:

let state = this.$store.state; let newState = {};  Object.keys(state).forEach(key => {   newState[key] = null; // or = initialState[key] });  this.$store.replaceState(newState); 

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 call resetStateA. Then you would clone the original state (that includes all the modules before resetting).

var currentState = deepClone(this.state) 

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 that

var newState = Object.assign(currentState, {   a: resetStateA }); 

and use that new state with replaceState, which includes the current state of all you modules, except the module a with its initial state:

this.$store.replaceState(newState); 

Original solution

I found this handy method in Vuex.store. You can clear all state quickly and painlessly by using replaceState, like this:

store.replaceState({}) 

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 and b, you'd do:

store.replaceState({   a: {},   b: {} }) 
like image 45
seebiscuit Avatar answered Nov 14 '22 04:11

seebiscuit