React native中的组建通知通信:

有这么一个需求,在B页面pop()回到A页面,需要A页面执行刷新,那么我们可以采用以下方法:

1:在A页面Push到B页面中,加上一个A页面中的刷新函数做为参数,然后在B页面中在pop()函数封装后通过this.props.xxx来执行!

2:通过组建之间的通知(监听)来执行!

 在A页面上:

在didmount中

this.listener = RCTDeviceEventEmitter.addListener('undateUserInfo',(value)=>{
// 接受到通知后刷新
console.log("宝宝接受到通知到啦")

//需要执行的函数
this._onRefresh();
});

在B页面上:

back(){

pop();

RCTDeviceEventEmitter.emit('undateUserInfo',value);

}

就这样就ok啦!当然ABye 面都需要倒入组建:import RCTDeviceEventEmitter from 'RCTDeviceEventEmitter'

注意:在A页面推出的时候,需要移除监听:

在unMount中

componentWillUnmount(){
this.listener.remove();
};

原文地址:https://www.cnblogs.com/allenxieyusheng/p/6693738.html