React通过
refattribute提供了一个您要尝试执行的操作的接口。给组件分配一个
ref,其
current属性将是您的自定义组件:
class Parent extends React.Class { constructor(props) { this._child = React.createRef(); } componentDidMount() { console.log(this._child.current.someMethod()); // Prints 'bar' } render() { return ( <div> <Child ref={this._child} /> </div> ); }}注意
:仅根据下面的文档,如果子组件被声明为类,则此方法才有效:https :
//facebook.github.io/react/docs/refs-and-the-dom.html#adding-a-
引用到类组件
更新2019-04-01: 更改了示例以使用类和createRef
每个最新的React文档。
2016年9月19日更新: 更改了示例,以
根据ref
String属性文档中的指南使用引用回调。



