美文网首页
react-父组件触发子组件的事件

react-父组件触发子组件的事件

作者: 小白修仙路 | 来源:发表于2019-06-12 11:02 被阅读0次

点击父组件触发子组件的方法

==<ChildDemo onRef = { this.onRef } />

<div onClick={this.Parent}>点击父组件触发子组件click事件</div>

onRef=()=>{

this.child = ref

}

Parent=()=>{

 this.child.childFun()    //childFun是定义在子组件的事件

}


定义子组件

constructor(props){

super(props)

this.props.onRef(this)     //此定义必须写

}

childFun=()=>{

 console.log("点击父组件触发子组件")

}

相关文章

网友评论

      本文标题:react-父组件触发子组件的事件

      本文链接:https://www.haomeiwen.com/subject/kvsrxctx.html