How to use React.useRef() in class component?

useRef() is among react hooks which are meant to be used in Functional components. But if you want to create a reference in a class-based component, you can do it from the class constructor like the code below:

  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }

OR
  constructor(props) {
    super(props);
    this.state = { myRef: React.createRef() }
  }

Check React.createRef().

Leave a Comment