I'm trying to log the click event on a button in react:
const InputBox = () => {
const clicky = fromEvent(
document.getElementById('clickMe'),
'click'
).subscribe(clickety => console.log({ clickety }));
return (
<button id="clickMe" type="button">
Click Me
</button>
);
};
I get the following error 'Invalid event target'
The setup seems to be fine. If I replace document.getElementById('clickMe')
with document
then it logs the clicks. But that logs any click in the document and I just want the clicks on the button in question.
I tried using a ref instead...
const InputBox = () => {
const buttonEl = React.useRef(null);
const clicky = fromEvent(buttonEl.current, 'click').subscribe(clickety =>
console.log({ clickety })
);
return (
<button ref={buttonEl} type="button">
Click Me
</button>
);
};
...but then I get the same 'Invalid event target' error.
Can someone help me understand why this is an invalid event target and how to fix this so that I can use fromEvent in react.
Update
The problem was that I did not register the observable when mounting the react components.
If you do this, you must also unmount the component when unmount.
This works for me now.
const InputBox = () => {
React.useEffect(() => {
const click$ = fromEvent(
document.getElementById('clickMe'),
'click'
).subscribe(clickety => console.log({ clickety }));
return () => click$.unsubscribe();
}, []);
return (
<button id="clickMe" type="button">
Click Me
</button>
);
};
RxJS fromEvent() operator is a creation operator used to give the output as an observable used on elements that emit events, such as buttons, clicks, etc.
You can do it on the component itself, or on it's parent and pass the value to it as props. A good practice is to handle the unsubscription on the componentWillUnmount event using the takeUntil operator and a Subject: unmount$ = new Subject() //component class property componentDidMount() { //if using rxjs 6: window.
subscribe() This method is used by React components to subscribe to messages that are sent to an observable.
Step 1: Create a React application using the following command: Step 2: After creating your project folder i.e. folder name, move to it using the following command: Step 3: After creating the ReactJS application, Install the required module using the following command: Project Structure: It will look like the following.
- GeeksforGeeks How to use rxjs module in ReactJS ? Rxjs stands for Reactive Extensions for JavaScript. This module provides the implementation of o bservable type to work with reactive programming which is an asynchronous programming paradigm.
In our React example the value of name came from a Promise. In RxJS land, defining the async nature of name is quite simple, we only have to create an Observable from fetchSomeName and handle weather the Promise is resolved or rejected in the following way:
In JavaScript there are no primitives for determining the async nature of this expression (neither in React). Let's get away from React for a while and let's see if we can express greeting (and fulfill our FRP definition) using RxJS.
Wrap it in useEffect()
that's when the dom is ready
const InputBox = () => {
const buttonEl = React.useRef(null);
useEffect(() => {
const clicky = fromEvent(buttonEl.current, 'click').subscribe(clickety =>
console.log({ clickety })
);
return () => clicky.unsubscribe();
}, []);
return (
<button ref={buttonEl} type="button">
Click Me
</button>
);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With