Closed dmicic closed 5 years ago
Ah, I saw now that the subscribe method returns a function which can be used to unsubscribe.
https://github.com/ryardley/ts-bus/blob/4fd905f0e17ea52803586620c462f342592d9666/src/EventBus.ts#L68
That is correct mind I close this? I am pretty sure the docs mention it but possibly don't cover it in detail.
Some random thoughts which went through my mind in regards to the subscription management.
Now let's say component B gets re-rendered to do some changes in the data. As a result, the code above will execute again and it will subscribe 'probably' for the 2nd time to events from component A. Will the bus still keep a reference to the old handler? If so, would it be better to use the useEffect() hook with the 'bus' dependency in order to avoid obsolete subscriptions?