Reviving nested states from localstorage results in loss of part of nested initial state values
See original GitHub issueSuppose I have this initial state:
{
someKey: {}
itemResults: {
items: [],
totalItems: 0,
isListView: true
},
someOtherKey: {}
}
and I have this config:
localStorageSync({
keys: [{itemResults: ['isListView']}],
rehydrate: true
})
and I have this state being revived from localStorage:
{
itemResults: {
isListView: false
}
}
Which means during @ngrx/store/init
, since we only do a shallow merge of keys using Object.assign, I end up with the following state:
{
itemResults: {
isListView: false
}
}
So basically if you use a nested key or keys, then that slice of state overrides the whole slice of initialState for the top level key. I was thinking it’d be nice if I could just use lodash’s merge to merge (or basically Object.assign nested keys also)<del>, but I don’t want to bloat this library either. It would be nice if I could supply a function to the library to use to do the merge, then I could just use lodash.merge there instead of bundling it with the library. </del>
<del>Could we possibly add a config option to allow the user to provide a custom merge function? Basically something like this?</del>
export const localStorageSync = (config: LocalStorageConfig) => (reducer: any) => {
// ... code omitted
const mergeFunction = config.customMergeFunction || ((state, rehydratedState) => Object.assign({}, state, rehydratedState));
return function (state = rehydratedState, action: any) {
if (action.type === INIT_ACTION && rehydratedState) {
state = mergeFunction(state, rehydratedState);
}
// ... code omitted
};
};
<del>Any thoughts/objections, anyone?</del>
EDIT: Renaming the thread since it appears that there’s a general consensus that a deep merge should be the default behavior here.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:9
- Comments:21
I don’t really understand how this library is usable without fixing this. If I want to sync only part of the state, it makes sense that the rest of the initial state will not be changed by default. Unless I’m missing something
What is preventing this issue from being fixed for more than a year? Any help needed?