import React, {Component} from 'react';
export default class Parent extends Component {
render() {
return(
<div>
<Child onRef={ref=>this.child = ref} />
<button onClick={this.click} >click</button>
</div>
)
}
click = (e) => {
this.child.myName()
}
}
class Child extends Component {
componentDidMount(){
this.props.onRef(this)
}
myName = () => alert('hello from child component')
render() {
return ('woqu')
}
}
总结:父组件调用子组件的方法,原理是子组件调用父组件的方法,参数是子组件的实例,这样可以在父组件得到子组件的
所有信息,包括子组件的方法
原文:https://blog.csdn.net/hesonggg/article/details/79373565