I have worked with Toast libraries before that use Redux. It is easy to envisage the ToastContainer subscribing to the store to be auto-updated with new toasts. But, I cannot see redux being used in this library, so how does it work?
Hello @joetidee,
React-toastify use a dead simple pubsub. So-called EventManager. It has 3 methods:
When the ToastContainer is mounted, it listens to 2 events, the callback relies on setState, there is no forceUpdate or any other evil stuff:
When you call toast('hello'), under the hood eventManager.emit(SHOW, toastOptions) is called.
When you call toast.dismiss(toastId), eventManager.emit(CLEAR, toastId) is called.
And so on.
Now one may ask why not using redux?
eventManager has only a few lines of code and has only 3 methods. If you want more detailed explanations do not hesitate.
Thanks for the explanation. Does EventManager set any window event listeners?
No, the EventManager use only custom event. There is only one DOM event used, it's the visibilitychange. This one is used to pause the pause when the window is not visible.
Most helpful comment
Hello @joetidee,
React-toastify use a dead simple pubsub. So-called
EventManager. It has 3 methods:When the
ToastContaineris mounted, it listens to 2 events, the callback relies onsetState, there is noforceUpdateor any other evil stuff:When you call
toast('hello'), under the hoodeventManager.emit(SHOW, toastOptions)is called.When you call
toast.dismiss(toastId),eventManager.emit(CLEAR, toastId)is called.And so on.
Now one may ask why not using redux?
eventManagerhas only a few lines of code and has only 3 methods.If you want more detailed explanations do not hesitate.