react 父组件调用子组件的值和方法

4,845 阅读1分钟

1.通过ref

在子组件上添加ref

 <ChildList ref="myInput"/>

然后就可以在父组件里通过 this.refs.myInput来获取了

eg:this.refs.myInput.state.name

2.通过回调函数

在子组件上添加回调

<ChildList ref={(child)=>{this.myChild = child}}/>

在父组件中通过 this.myChild调用子组件方法

eg: this.myChild.handleClick(); //调用子组件方法