0

Normally we can use createRef() in react class based component by writing this.myRef = React.createRef(); insidide the constructor.

My question is:

Is there any way to use ref in function based component ?

Here is the conventional way to use refs in class based component:

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }
  render() {
    return <div ref={this.myRef} />;
  }
}
Shamim
  • 635
  • 2
  • 12
  • 28
  • https://stackoverflow.com/questions/54620698/whats-the-difference-between-useref-and-createref – Oleg Sep 16 '19 at 06:31
  • 1
    React has [useRef](https://reactjs.org/docs/hooks-reference.html#useref) hook for that – winwiz1 Sep 16 '19 at 06:53

0 Answers0