如何在基于功能的组件中使用引用

时间:2019-09-16 06:28:50

标签: reactjs react-component react-ref

通常,通过在构造函数中编写createRef(),可以在基于React类的组件中使用this.myRef = React.createRef();

我的问题是:

在基于函数的组件中可以使用ref吗?

以下是在基于类的组件中使用引用的常规方法:

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }
  render() {
    return <div ref={this.myRef} />;
  }
}

0 个答案:

没有答案