Introduce a synchronizeState
operator to allow syncing state between composed reducers easier.
#2220
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is motivated by the need to have different reducers that are composed together keep a synced up state.
Consider you have a reducer that is composed of different sub-reducers, there are cases when you have some piece of state which need to be accessed (and maybe even mutated) by a couple of them. Composing a state which can be scoped/pulledback to allow each and everyone of them to work off of the same piece of state can prove to be challenging. An easier approach for those times is to have local copy of the state and manually try to keep it all in sync via the parent reducer.
This update is syntactic sugar to remove the boilerplate needed to achieve that effect
Example usage:
This would ensure that whenever
sharedState
changes onParent
it also syncs with the copy help byChild
With a bunch of different child reducers you just avoid the issue of keeping it all up to date. I think this should scale much easier.
Limitation
A limitation of this approach is that you are indeed maintaining multiple state copies so if the state is "heavy" you could use up a lot of memory.