react native 之 事件监听 和 回调函数

转载自 http://www.cnblogs.com/shaoting/p/6565339.html

同原生一样,react native 同样也有事件监听和回调函数这玩意.

场景很多,比如:A界面push到B界面,B界面再pop回A界面,可以给A界面传值或者告诉A刷新界面.

事件监听

事件监听类似于iOS原生的通知,一个发,一个收即可.

A界面收:

 import {
     DeviceEventEmitter
 } from 'react-native';
componentDidMount(){

//收到监听
this.listener = DeviceEventEmitter.addListener('通知名称',(e)=>{
    console.log(e);
});
}
componentWillUnmoun(){
//移除监听
this.listener.remove();
}

 

B界面在pop回A界面的时候发:

 

import { DeviceEventEmitter } from 'react-native';

AAA=()=>{

const {navigation} =this.props;

const  value='你好';

DeviceEventEmitter .emit('通知名称',value);

navigation.pop({})

}

 

事件回调 

A界面在push到B界面的时候定义个回调函数

push = () =>{
    this.props.navigator.push({
         component:DetailsView,
         passProps:{
             callback:(msg)=>{ alert(msg) }
         }
     })
 }

 

B界面在pop回A界面的时候调用该回调函数

 

pop = () =>{
    this.props.navigator.pop({
     })
 
     if(this.props.callback){
         this.props.callback('回调')
     }
 }

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值