react 父組件調用子組件的值和方法

1.經過ref函數

在子組件上添加refthis

 <ChildList ref="myInput"/>
回調函數

而後就能夠在父組件裏經過 this.refs.myInput來獲取了List

eg:this.refs.myInput.state.name方法

2.經過回調函數回調

在子組件上添加回調

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

在父組件中經過 this.myChild調用子組件方法

eg: this.myChild.handleClick(); //調用子組件方法

相關文章
相關標籤/搜索