store.registerModule overwrites persisted state
See original GitHub issueConsider this example code:
const store = new Vuex.Store({
plugins: [createPersistedState(persistedStateOptions)]
});
import user from './user.js';
store.registerModule('user', user);
This will load the persisted state from localStorage (or other) and then it will be overwritten by the state defined in the “user” module. I think I understand why this is happening, but a small warning about this in the documentation could come in handy for some people.
This on the other hand will work fine:
import user from './user.js';
const store = new Vuex.Store({
modules: {
user
},
plugins: [createPersistedState(persistedStateOptions)]
});
I can use the latter in my project, but I started out with the first implementation and spent some time finding out what prevented the persisted state from being restored.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:10 (3 by maintainers)
Top Results From Across the Web
Making only one module persistent with vuex-persistedstate
Looking at the API docs, you will need to configure the plugin to only persist a certain subset of the store. export default...
Read more >vuex-persist - npm
When using store.registerModule you risk the (restored) persisted state being overwritten with the default state defined in the module ...
Read more >Modules | Vuex
When you set preserveState: true , the module is registered, actions, mutations and getters are added to the store, but the state is...
Read more >Switch to persistent Vuex
store._modulesNamespaceMap = Object.create(null);. var state = store.state;. // init all modules. installModule(store, state, [], store.
Read more >https://raw.githubusercontent.com/championswimmer/...
Choose which mutations trigger store save, and which don't, ... registerModule` you risk the (restored) persisted state being overwritten with the default ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
just wondering if there are any possibility to support to restore state from dynamic module registration…? my vuex structure heavily depends on dynamic module and it would be great if restoring the state is possible 😄
This is how I got it to work with dynamic modules:
Two things to note here: When the state is restored you will need to pass
preserveState: true
so that you don’t override the persisted state. And to avoid registering the module twice you can add a customProp to the module objectDiscoveryStoreModule.registered