React中父组件执行子组件的函数 使用class + onRef写法
在React中,父组件执行子组件的函数的写法如下
父组件中
添加函数
onRef = (ref) => {
this.child = ref
}
在使用子组件时
<ChildComponet onRef={this.onRef} />
onRef是固定的用法,
在子组件里
componentDidMount(){
this.props.onRef(this)
}
父组件执行子组件的函数
submitFun = () => {
this.child.onReset();
}