⁉[DISCUSSION]: Should the state from storage override or merge with an updated default state #1910
Replies: 10 comments
-
I have experienced this exact same thing, but just nuked my storage to fix the issue. I'm not sure that there is an easy answer to this, so I made this a DISCUSSION issue. Currently, storage state just overrides the default state. I think that we could provide a mechanism for letting the developer choose how the storage gets merged with the default state.... maybe an addition to the new Also, I'm not sure about where HMR fits in with this? @splincode? |
Beta Was this translation helpful? Give feedback.
-
I don't think it should override the storage. I think it should more be like a fallback. Like: let storageState = {
property1: "bla"
}
let defaultState = {
property1: "default",
newProperty: "default"
}
let new State = {
...defaultState,
...storageState
}
// Outcome
// {
property1: "bla",
newProperty: "default"
// } It should not remove Properties, as this would potentially break something. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Thanks, I will have a look at it. Maybe I can add my implementation to this library than :) |
Beta Was this translation helpful? Give feedback.
-
So similar of what @DanielHabenicht described I guess. Thanks to the new 'afterDeserialize' option, it is now possible to change this behavior if needed. |
Beta Was this translation helpful? Give feedback.
-
I just had an extra tought about this. It depends if we use the "deeply nested via dot notation" or not. If we do, then logically I would do what I describe in my previous message. Otherwise, I would use the whole state contained in the storage and ignore the default values. So for me there is a difference between the behaviors depending on the intention:
|
Beta Was this translation helpful? Give feedback.
-
Some issues I have run into:
|
Beta Was this translation helpful? Give feedback.
-
@DanielHabenicht Just to note that the |
Beta Was this translation helpful? Give feedback.
-
Ohhh, ok. I will take a look at Dev then. The documentation and implementation is different anyway. Which one is right? ( |
Beta Was this translation helpful? Give feedback.
-
We ended up creating an object which must be updated once a part of a cached store is changed structurally. We keep this object in localStorage as well. Then on application initialization we check whether local version of this object is different from the cached in localStorage one, and if so - we update localStorage object while logging user out and resetting the state 🙈 There's page reload happening when user logs in (kind of technical debt) and thus the store gets initialized from scratch with default values (due to localStorage is reset).
In
|
Beta Was this translation helpful? Give feedback.
-
Affected Package
The issue is caused by package @ngxs/storage-pluginIs this a regression?
No (idk)
Description
The default value is not present if a new property is added to a state that was persisted to local storage before.
If a property is not in local storage it should fall back to the default values defined in the state.
🔬 Minimal Reproduction
Use it in your app in conjunction with
@ngxs/storage-plugin
and persist the state in the localstorage.Add another property to the state:
undefined
.Reproduction:
https://stackblitz.com/edit/ngxs-empty-action-handler-bcvdty?file=src%2Fapp%2Fapp.component.ts
Environment
Beta Was this translation helpful? Give feedback.
All reactions