父组件
handleChildEvent = (ref) => {
// 将子组件的实例存到 this.childRef 中, 这样整个父组件就能拿到
this.childRef = ref
}
onGetUserInfo=async()=>{
this.childRef.Replenishmentred()
}
<ProductItem onRefush={() => {
onChildEvent={this.handleChildEvent}
/>
<button onClick={this.onGetUserInfo}> 点击事件</button>
子组件
componentDidMount () {
// 在子组件中调用父组件的方法,并把当前的实例传进去
this.props.onChildEvent(this)
}
Replenishmentred = async fun => {
console.log("成功在父组件调用了子组件的事件')
}
真实项目实例 可以用的
子组件调用父组件事件
子组件
//事件中
this.props.redenvelope('redenvelopeto', trdto.data.rewardMoney)
<ProductItem cls={cls} onRefush={() => {
redenvelope={this.redenvelope}
/>
父组件
redenvelopeto= async (heep) => {
this.setState({
heep
})
}