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} />;
}
}