可以使用 React.createRef()
创建 Refs 并通过 ref
属性附加到 React 元素。它通常在创建组件时分配给实例属性,然后可以在整个组件中引用。
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.callRef = React.createRef();
}
render() {
return <div ref={this.callRef} />;
}
}