/ 5 min read
Sync state updates b/w 2 components that use a common hook
There are some cases where there is a need to share a common state between 2 different instances of the same hook used in 2 different components which can be placed at any level in the hierarchy.
There are certainly many ways to acheive this, one common approach would be to use ContextAPI but, using ContextAPI for such a scenario easily makes code messy and the API itself is over-engineered to be used in this scenario. And one obvious solution to this is using a state management tool like mobX or redux. But the point here is to write minimal code and acheive this without using any third party libraries.
So what if we had a way to notify each instance that a state has changed in one of the instance and the others should update their local-state accordingly. This can be possible only if we had a callback to trigger whenever a state change event is triggered in any one of the instances.
This is a typical case of one-to-many relationship where, if one object is modified, its depenedent objects are to be notified automatically. This is where the Observer pattern is used.
It specifies communication between objects: observable
and observers
. An observable is an object which notifies observers about the changes in its state.
Observer is a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.
Observer pattern falls under behavioral pattern category.
To get started let us begin by creating a custom hook that hosts a local state which will get set on a button click and reset after a 2s time delay.
Now let’s define our App component which renders 3 buttons which use the above hook to trigger a blocking process.
Out of the 3 components that render a button let’s say we want to share the state between ComponentA
and ComponentD
. To do that lets build our Observerable object.
We need to import this in our custom hook and create an Observable object instance as below.
We can then use this to subscribe our state update calls.
This completes our custom hook that is updated with a boolean config shouldSubscribeForGlobalChanges
which provides an option to subscribe to global changes or not. If subscribed then the isLoading
will
be updated accordingly when there is a button click on any of the hook’s instance.
Now lets pass this config from our components A and D.
By running this, we can see that on click of button A button D gets a loading text while button C remains un affected. This is because, component A and D are registered but component C isn’t.
That’s the end of this blog, which showed you how 2 instances which hosts a local state can be notified of a state change in either one of the components.
Thanks for reading..!!!!