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.

rehydrate State is not working with FeatureModule state

See original GitHub issue

I have a FeatureModule for Authentication that is using StoreModule.forFeature(‘auth’ and therefore creates an auth object on the store. This auth object i want to sync with the localStorage. The rehydrate function only listens on the @ngrx/store/init event. However at this time the auth if not yet set on the store You have to also listen for the @ngrx/store/update-reducers event. This is called afterwards (for each FeatureModule) and overwrites the auth with it’s initialState value.

So the whole ngrx-store-localstorage module rehydrate is not working for FeatureModules (StoreModule.forFeature) But actually thats the whole point of this so it is not useable…

Update: I just diged in it and it seems, that rehydrate actually gets called for every added FeatureModule on the @ngrx/store/update-reducers event. However in the meanwhile, the store got synced again to the localstorage and has overwritten the auth. So the thing is, that when the event is @ngrx/store/update-reducers then please don’t sync it to the localStorage as it overwrites the state that should get rehydrated

Update 2: I made a fixed version of localStorageSync, it may not be very sophisticated and it actually has to cover more configuration, but for me it does the trick for now: `export const localStorageSyncFixed = (config: LocalStorageConfig) => (reducer: any) => {

if (config.storage === undefined) { config.storage = localStorage || window.localStorage; }

if (config.storageKeySerializer === undefined) { config.storageKeySerializer = (key) => key; }

const stateKeys = validateStateKeys(config.keys); const rehydratedState = config.rehydrate ? rehydrateApplicationState(stateKeys, config.storage, config.storageKeySerializer) : undefined;

return function (state = rehydratedState, action: any) { /* Handle case where state is rehydrated AND initial state is supplied. Any additional state supplied will override rehydrated state for the given key. */ if (action.type === INIT && rehydratedState) state = Object.assign({}, state, rehydratedState);

// Also on Update Reducers rehydrate the state
if (action.type === UPDATE && rehydratedState)
  state = Object.assign({}, state, rehydratedState);

const nextState = reducer(state, action);

if (action.type !== UPDATE && action.type !== INIT)
  syncStateUpdate(nextState, stateKeys, config.storage, config.storageKeySerializer, config.removeOnUndefined);

return nextState;

}; };`

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
Hainesycommented, Apr 18, 2019

I’m solving with the following:

export const storageSync = (featureName: keyof State, initialState: object) => (parentReducer: ActionReducer<object, Action>) => {
  const reducer = (state: object, action: Action): object => {
    if (action.type === '@ngrx/store/update-reducers') {
      state = Object.assign({}, initialState, state);
    }
    return parentReducer(state, action);
  };

  return localStorageSync({
    keys: generateSyncKeys(initialState),
    rehydrate: shouldHydrate(currentPathname),
    storageKeySerializer: storageKeySerializer(featureName)
  })(reducer);
};
1reaction
nickwingercommented, May 24, 2018

@darbio No, i‘m not using this functionality. I‘m doing it manually with localstorage at the initial state

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngrx/store - Gitter
The example app does not persist state during refresh. But the general issue is that a feature modules's initial state is currently ignored/lost...
Read more >
How to Keep NgRx State on Refresh - DEV Community ‍ ‍
Learn how to keep the state of the NgRx store between page reloads with Redux devtools and re-hydration from localStorage.
Read more >
Rehydrating NgRx State from Universal - YouTube
Presented by WWCode San DiegoSpeaker: Jay Bell (@JayCooperBell)Women Who Code San Diego and San Diego Angular Meetup meet the third ...
Read more >
NgRx - Runtime checks
strictStateImmutability : verifies that the state isn't mutated ... This check is important to be able to work with the state in a...
Read more >
Retaining State of The Angular App When Page Refresh With ...
The solution for the above problem is that when you refresh the page or reload the page you are saving the application state...
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