ngrx-store-localstorage
ngrx-store-localstorage copied to clipboard
ngrx-store-localstorage doesn't play well with createFeature
I'm using NGRX's createFeature
for some of my store feature slices. However, when I use ngrx-store-localstorage to save anything from those feature slices, the merge reducer overwrites my initial state from that feature with rehydrated object, resulting in my feature not having an initialState
. I didn't deep dive enough, but I think the reason is that feature modules are added to the store a bit later that the first @ngrx/store/init action
runs.
I've created a workaround by defining my own merge reducer that skips rehydrated state's top level keys if they're not in the store yet.
mergeReducer: (existingState, rehydratedState, action) => {
//remove rehydratedState top level keys that are not in existingState using Object.keys
const definedRehydratedState = Object.keys(rehydratedState).reduce((acc, key) => {
if (existingState[key] != null) {
acc[key] = rehydratedState[key];
}
return acc;
}, {});
return defaultMergeReducer(existingState, definedRehydratedState, action);
}