btroncone / ngrx-store-localstorage

Simple syncing between @ngrx store and local storage

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rehydrating Feature Module using ngrx/entity

sonnyvesali opened this issue · comments

So I have a user module that uses ngrx/entity to store state, and based on the already raised issues that seem to raise similar concerns and address them like this one I can't get the state to persist in the feature module despite following the prescription of the code provided.

I've made a sessionStorage.ts with the following code

import { ActionReducer } from '@ngrx/store';
import { LocalStorageConfig, localStorageSync } from 'ngrx-store-localstorage';

export function sessionStorage(
  reducer: ActionReducer<any>
): ActionReducer<any> {
  const config: LocalStorageConfig = {
    keys: ['register'],
    rehydrate: true,
    removeOnUndefined: true,
    storageKeySerializer: (key: string) => `user_${key}`,
  };
  return localStorageSync(config)(reducer);
}

As well as the following code in my feature module

import {StoreModule} from '@ngrx/store';
import {userReducers} from './redux/module-reducer';
import {sessionStorage{ from './redux/session.storage';

@NgModule({
imports: [
  StoreModule.forFeature('user', userReducers, {
      metaReducers: [sessionStorage],
      initialState: [sessionStorage],
      //i've tried `metaReducers, initialState: [sessionStorage]`
      // but I got a no value exists error for the metareducer
]
})

I'm not really sure what I'm doing wrong for the state not to persist. The data is being saved in to localStorage it just isn't rehydrating the data store upon refresh, it's just going to the default entity structure I laid out in the reducer. If I need to show more code to get to the root of the problem, let me know, thanks.

@sonnyvesali did you find a solution to your problem?