Get a reference to an element in React for using 'fromEvent' method of rxjs

Viewed 53

In a React project that uses rxjs, I need to get a reference to an HTML element. I am using React.createRef:

const buttonWait = React.createRef();
<button ref={buttonWait}> Wait </button>

But { current: null } is printed to the console. How to get a link to an element to add to fromEvent and subscribe?

1 Answers

According to React's documentation on refs, React.createRef() is not to be used in functional components:

You may not use the ref attribute on function components because they don’t have instances.

Consider using useRef() instead:

import { useRef } from "react";
const buttonWait = useRef(null);
<button ref={buttonWait}> Wait </button>
Related