React Native通过发布事件和订阅事件的机制来使得native和JavaScript通信,个人感觉类似于OC中的发送消息。
下面是使用RCTDeviceEventEmitter的方法。
一、在OC代码中
1、#import "RCTEventDispatcher.h"
2、@implementation下 @synthesize bridge = _bridge;
3、在需要发布事件的地方调用下面的函数:
_bridge.eventDispatcher sendDeviceEventWithName:(NSString*)eventName body:(id)bodyData
//eventName :事件的名字 bodyData:要传递的数据
二、JS代码中
1、var RCTDeviceEventEmitter = require(‘RCTDeviceEventEmitter’);`
2、在需要的地方订阅事件。比如在组件挂载完成后订阅:
componentDidMount: function() {
this.subscription = RCTDeviceEventEmitter.addListener('orientationDidChange',(dimensions) => {
console.log(dimensions);
})
},
3、记得移除订阅
componentWillUnMount: function() {
this.subscription.remove();
},
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- esig.cn 版权所有 湘ICP备2023023988号-3
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务