question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

store.registerModule overwrites persisted state

See original GitHub issue

Consider 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
japboycommented, Aug 10, 2019

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 😄

3reactions
salam0smycommented, Mar 19, 2020

This is how I got it to work with dynamic modules:

if (!DiscoveryStoreModule.registered) {
        store.registerModule('discovery', DiscoveryStoreModule, {
          preserveState: !!store.state['discovery'],
        });
        DiscoveryStoreModule.registered = true;
}

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 object DiscoveryStoreModule.registered

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found