- You're required to communicate between components in different locations at the component hierarchy. Perhaps they aren't even in a parent child relationship
- Some functions are involved, maybe some loops. This code is already done, and already works.
- Perhaps, some refs or other outside-of-React escape hatches are involved. This code is already done, and already works
- And of course the usual restriction of a "professional" software developer -- don't change too much, and anything you change must be regression tested and so on and so on!
- Since functions and loops are involved, you can't use hooks (at least not inside the functions or loops)
- The code is already done; you don't want to refactor everything and besides you're not sure that refactoring everything will lead to a better solution anyway because it's a one-off
- Ideally all the information should have been stored in a central location and flow down to all the required components, but that bus is gone
- Besides, the days of storing a property, modifying it with two-way data binding and binding to that property are long since over (this isn't Knockout or Backbone!)
- Besides, even if you did it, you would have to modify enormous amounts of already completed code, possibly create regressions, and pass down props many levels!
- Seems like a perfect situation to use Redux (Redux Toolkit) or some other state management but besides the fact you can't use hooks, you don't want to mess with the global store and it seems wrong to use it for the one-off
- You look at some solutions like HTML5 local storage, but that's already outside of React and you would somehow have to subscribe to local storage anyway
- Besides, the usehook-ts package doesn't properly install
- Besides, after copying the usehooks-ts package, it's a hook, so you can't use it in the functions anyway!
JAVASCRIPT
The dispatchEvent()
method of the EventTarget
sends an Event
to the object, (synchronously) invoking the affected event listeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent()
.
Calling dispatchEvent()
is the last step to firing an event. The event should have already been created and initialized using an Event()
constructor.
The
dispatchEvent()
method of theEventTarget
sends anEvent
to the object, (synchronously) invoking the affected event listeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually withdispatchEvent()
.Calling
dispatchEvent()
is the last step to firing an event. The event should have already been created and initialized using anEvent()
constructor.