So you want to Integrate Observables with React? - deanrad/rx-helper GitHub Wiki
Scenario: You have Observables, and want to use their values in React, using Hooks.
Solution: Subscribe to those Observables and push out their values to the channel
(which you can think of as a variant of a Subject), using trigger
- providing a name to go in the type
field of the event. The Observable values will go in the payload
. Then pass a function to useListener
which will react to each named event.
Bonus: You can match on string, Boolean, regex, or function predicate, and call useListener
as many times as you like - So if you want to log to the console, just try useListener(true, ({ event }) => console.log(event))
! And of course, any Observable being listened to is torn down once the component is unmounted.
See the CodeSandbox for a full example.