Merge states instead of overriding it.
See original GitHub issueCurrent behaviour
When I pass something like:
{
keys: [
{
app: ['locale']
}
],
rehydrate: true
}
It will override all my initial app
state keys, and only fills in the locale
field.
This will break my app, and forces me to choose to either sync the wholeapp
key, or nothing at all. Or alternatively, make an entirely new locale
reducer on the root.
Expected behaviour
The state being merged similar to how it happens in a reducer:
{
[currentKey]: {
...initialState[currentKey],
...localStorage[currentKey]
}
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:6 (1 by maintainers)
Top Results From Across the Web
React useState overwrites state object instead of merging
Instead of merging changes I make to a state object, React is overwriting it completely. Could this be caused by the context? I'm...
Read more >Git is overwriting my code on merge. If someone else ... - Quora
No. It just updates the knowledge your local git has about the latest state of the remote. It does not overwrite local changes....
Read more >How State Updates Are Merged in React | by Robin Kim
setState( ) is a method that is available to all React components. Invoking it lets React know that the component state has been...
Read more >Customizing the behavior of cached fields - Apollo GraphQL
When the write occurs, the field's new value is set to the merge function's return value, instead of the original incoming value.
Read more >Merge step overwriting shared vars? - SAS Communities
Solved: Hi, I have two datasets that both have ID and a number of other variables. Some of these variables exist on both...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Closing this issue as it was fixed in version 6.0.0 which started doing a deep merge of the initial state and the local storage state.
Not sure if there is an “easy” way to have access to the initial state of all reducers, but adding an
initialState
object toLocalStorageConfig
, and then merging this object withstate
andrehydratedState
in the initial action might be the easiest until a better solution is found. Not sure if this solution would be satisfactory, but it solved the issues i were having.I can put up a quick PR if you think its worth the time #84I have closed my PR again, since i think this problem could be solved by #65