The fromEvent
function in rxjs
library can be used to create an observable stream of events from a DOM element or node.js EventEmitter.
Here is an example of how to use fromEvent
to listen for a 'click' event on a button element:
index.tsx201 chars9 lines
In the above code, we import the fromEvent
function from the rxjs
library. We select a button element from the DOM and pass it along with the event type 'click' to fromEvent
. This returns an observable stream of 'click' events on the button.
We then subscribe to this stream with a callback function that logs the event object to the console whenever the button is clicked.
Similar to 'click', we can use any other standard DOM event type (like 'mousemove', 'keydown', etc.) or any custom event type emitted by a node.js EventEmitter, with the fromEvent
function.
gistlibby LogSnag