Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialisation for react-native RCTBridgeModule

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

My current solution is to declare a method which has to be called inside the 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
like image 390
n1ru4l Avatar asked Dec 12 '25 19:12

n1ru4l


1 Answers

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;
}
like image 154
n1ru4l Avatar answered Dec 14 '25 12:12

n1ru4l



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!