Introduce a synchronizeState operator to allow syncing state between composed reducers easier #2224
raghavbhasin97
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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.
Package
Link to the repo if anyone wants to use this as an add-on library
Beta Was this translation helpful? Give feedback.
All reactions