Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use RXJS fromEvent in React?

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'

enter image description here

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>
  );
};
like image 904
Josh Pittman Avatar asked Oct 14 '19 03:10

Josh Pittman


People also ask

What is FromEvent in RxJS?

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.

How do I use Behaviorsubject in React?

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.

Can we use observable in React?

subscribe() This method is used by React components to subscribe to messages that are sent to an observable.

How to create a ReactJS application?

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.

What is RxJS module in ReactJS?

- 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.

Where does the value of name come from in ReactJS?

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:

Can We express greeting in RxJS using JavaScript?

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.


Video Answer


1 Answers

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>
  );
};
like image 148
Fan Cheung Avatar answered Oct 21 '22 20:10

Fan Cheung