How do I use RXJS fromEvent in React?

后端 未结 2 675
故里飘歌
故里飘歌 2021-01-14 23:34

I\'m trying to log the click event on a button in react:

const InputBox = () => {
  const clicky = fromEvent(
    document.getElementById(\'clickMe\'),
           


        
2条回答
  •  [愿得一人]
    2021-01-15 00:20

    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 (
        
      );
    };
    

提交回复
热议问题