RCTEventEmitter使用

时间:2022-04-27
本文章向大家介绍RCTEventEmitter使用,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在0.27版本之前,RN的Native端向js端发射消息主要通过sendDeviceEventWithName的方式,相关代码如下。

@synthesize bridge = _bridge;  

-(void)iseCallback:(NSString*)code result:(NSString*) result  
{  
  [_bridge.eventDispatcher  
   sendDeviceEventWithName:@"iseCallback"  
   body:@{  
          @"code":code,  
          @"result":result,  
          @"index":self.bridgeIndex,  
          @"category":self.bridgeCategory  
          }];  
}  

然后Js端接受代码如下:

import RCTDeviceEventEmitter from 'RCTDeviceEventEmitter';  

this.listener = myNativeEvt.addListener('iseCallback', this.iseCallback.bind(this));  

this.listener && this.listener.remove();  

使用bridge.eventDispatche拦截消息时,有三分方法需要注意。

  • sendAppEventWithName
  • sendDeviceEventWithName
  • sendInputEventWithName 然后在JS那边也有三个对应的接收接口。
  • RCTAppEventEmitter
  • RCTDeviceEventEmitter
  • RCTInputEventEmitter

但是在0.28版本之后,但是在Xcode一直提示这种方式可能要被取代。

'sendDeviceEventWithName:body:' is deprecated: Subclass RCTEventEmitter instead

RCTEventEmitter用法

在0.28版本之后,ios向Js端发射消息的代码如下。 1、自定义的模块类头文件要继承自RCTEventEmitter。

#import <React/RCTEventEmitter.h>

#define RNIOSExportJsToReact(noti) [[NSNotificationCenter defaultCenter] postNotificationName:@"event-emitted" object:noti];

@interface RNIOSExportJsToReact : RCTEventEmitter<RCTBridgeModule> 
@end

在具体的原生代码中发送消息。

#import "RNIOSExportJsToReact.h"

@implementation RNIOSExportJsToReact

RCT_EXPORT_MODULE();

- (NSArray<NSString *> *)supportedEvents {
    return @[@"SpotifyHelper"]; //这里返回的将是你要发送的消息名的数组。
}
- (void)startObserving
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(emitEventInternal:)
                                                 name:@"event-emitted"
                                               object:nil];
}
- (void)stopObserving
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)emitEventInternal:(NSNotification *)notification
{
    [self sendEventWithName:@"SpotifyHelper"
                       body:notification.object];
}

+ (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"event-emitted"
                                                        object:self
                                                      userInfo:payload];
}

@end

一旦RCT_EXPORT_MODULE();声明该类是EXPORT_MODULE,那么该类的实例已经创建好了。然后我们在RN端调用以下即可。

import React,{Component} from 'react';
import {
  ·
  ·
  ·
  NativeModules,
  NativeEventEmitter,
} from 'react-native';

import Main from './app/HJMain';

var nativeBridge = NativeModules.RNIOSExportJsToReact;//你的类名
const NativeModule = new NativeEventEmitter(nativeBridge);

export default class Haojia extends React.Component {

  render() {
    return (
        <Main />
    );
  }

  componentDidMount(){
    NativeModule.addListener('SpotifyHelper',(data)=>this._getNotice(data));
  }

  _getNotice (body) {//body 看你传什么
      this.forceUpdate();//重新渲染
  }

  componentWillUnmount() {
    //删除监听
    this.NativeModule.remove()
  }

}

如果需要调用RN的方法,在OC需要的地方加上RNIOSExportJsToReact(参数)即可。