react-native RCTBridgeModule 的初始化 [英] Initialisation for react-native RCTBridgeModule

查看:50
本文介绍了react-native RCTBridgeModule 的初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道向我的 RCTBridgeModule 添加初始化步骤的最佳方法是什么.

I am wondering what is the best way for adding a initialisation step to my RCTBridgeModule.

我目前的解决方案是声明一个必须在 AppDelegate 内部调用的方法.

My current solution is to declare a method which has to be called inside the AppDelegate.

是否有任何其他/更好的解决方案不需要模块的最终使用者向其 AppDelegate 添加代码?

Are there any other/better solutions that would not require end consumers of the module to add code to their AppDelegate?

MyModule.h

#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
@interface MyModule : RCTEventEmitter <RCTBridgeModule>

- (void) initialize;

@end

MyModule.m

#import "MyModule.h"

@implementation MyModule

RCT_EXPORT_MODULE();

- (void) initialize
{
  // do some stuff
}

@end

推荐答案

我通过覆盖 init 方法使其工作.这现在也有记录(有点https://facebook.github.io/react-native/docs/native-modules-ios#dependency-injection).

I got it working by overriding the init method. This is now also documented (kind of https://facebook.github.io/react-native/docs/native-modules-ios#dependency-injection).

- (instancetype)init
{
  self = [super init];

  NSLog(@"Do some stuff here");

  return self;
}

这篇关于react-native RCTBridgeModule 的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆