码迷,mamicode.com
首页 > 其他好文 > 详细

React native中的组建通知通信:

时间:2017-04-11 17:26:33      阅读:772      评论:0      收藏:0      [点我收藏+]

标签:之间   mit   推出   imp   宝宝   函数   移除   listener   com   

有这么一个需求,在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();
};

React native中的组建通知通信:

标签:之间   mit   推出   imp   宝宝   函数   移除   listener   com   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!