![3a4802acc1cf842a0b9ab50f3f75da33.png](https://img-blog.csdnimg.cn/img_convert/3a4802acc1cf842a0b9ab50f3f75da33.png)
子组件可以触发父组件事件,同样父组件也可以触发子组件事件
大概理解:子组件初始化时将this传给父组件,父组件通过子的this触发到子组件方法
1、子组件componentDidMount生命周期下通过props传给父组件
componentDidMount() {
this.props.FunCenter(this);
}
//要执行的方法
FunCount(){
console.log("123")
}
2、父组件接收子组件的this
//保存子组件this到this.$child中给当前父组件使用
FunCenter = (_this) => {
this.$child = _this;
};
//父组件某个方法
FunRecovery() {
this.$child.FunCount();//执行子组件方法
}
render() {
return (
<Drag FunCenter={this.FunCenter}>
)
}
建议从子组件顺着思路走,走一遍就很清晰父组件是怎么触发子组件方法了