Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ngrx-store-localstorage doesn't play well with createFeature #268

Open
vvolodin opened this issue Aug 20, 2024 · 0 comments
Open

ngrx-store-localstorage doesn't play well with createFeature #268

vvolodin opened this issue Aug 20, 2024 · 0 comments
Labels
feature stores Functionality related to feature stores / modules known workaround There is a known workaround for this issue

Comments

@vvolodin
Copy link

vvolodin commented Aug 20, 2024

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);
      }
@BBlackwo BBlackwo added feature stores Functionality related to feature stores / modules known workaround There is a known workaround for this issue labels Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature stores Functionality related to feature stores / modules known workaround There is a known workaround for this issue
Projects
None yet
Development

No branches or pull requests

2 participants