React-native: RCTEventEmitter bridge is not set

Created on 9 Aug 2017  路  9Comments  路  Source: facebook/react-native

Is this a bug report?

Not confirm.

Have you read the Contributing Guidelines?

Yes, I have follow all sept of this document of how to use RCTEventEmitter to send event from Native to JS.but still meet this error.

Environment

  1. react-native -v: 0.45.1
  2. node -v: 8.1.2
  3. npm -v: 5.30

Then, specify:

  • Target Platform: iOS

Steps to Reproduce

  1. Create a helper class that can emit event from iOS native to JS according to the document.
  2. invoke sendEventWithName method any where in the code we need to emit event.

Expected Behavior

JS will receive the event and catch the data.

Actual Behavior

[tid:com.facebook.react.ShadowQueue] Exception 'bridge is not set. 
This is probably because you've explicitly synthesized the bridge in RCTCameraEvent, 
even though it's inherited from RCTEventEmitter.'
was thrown while invoking capture on target CameraManager with params (
        {
        audio = 0;
        barCodeTypes =         (
        );
        description = "";
        mirrorImage = 0;
        mode = 1;
        playSoundOnCapture = 1;
        preferredTimeScale = 30;
        quality = 0;
        target = 1;
        title = "";
        totalSeconds = 10;
        type = 1;
    },
    3892,
    3893
)

Reproducible Demo

EventHelper.h

#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

@interface RCTCameraEvent : RCTEventEmitter<RCTBridgeModule>

@end

EventHelper.m

#import "RCTCameraEvent.h"

@implementation RCTCameraEvent

RCT_EXPORT_MODULE();

- (NSArray<NSString *> *)supportedEvents
{
    return @[@"CustomEvent"];
}

- (void)sendEvent2JS
{
    [self sendEventWithName:@"CustomEvent" body:@{@"name": @"111"}];
}

@end

invoke send event method in code

RCTCameraEvent * cameraEvent = [[RCTCameraEvent alloc]init];
[cameraEvent sendEvent2JS];

All I want to do is create a helper class so that I can invoke the send event method anywhere I need.

I have look into many issues (#8714) and search related answer on Google and stackoverflow but still can not found a perfect solution using RCTEventEmitter. there are lots of developers like me meet this problem and react native haven't provide us a standard code demo or completely document.

Stale

Most helpful comment

Finally, I got the solution.

#import "RNNotification.h"
@implementation RNNotification

RCT_EXPORT_MODULE();

+ (id)allocWithZone:(NSZone *)zone {
    static RNNotification *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [super allocWithZone:zone];
    });
    return sharedInstance;
}

- (NSArray<NSString *> *)supportedEvents
{
    return @[@"EventReminder"];
}

- (void)sendNotificationToReactNative
{
    [self sendEventWithName:@"EventReminder" body:@{@"name": @"name"}];
}

When you use it!

RNNotification *notification = [RNNotification allocWithZone: nil];
[notification sendNotificationToReactNative]

All 9 comments

You shouldn't init RCTCameraEvent yourself. It will be initialized by react native. You must try to communicate with that instance in some way. For example your AppDelegate may send a notification which RCTCameraEvent subscribes to on init. You may then forward that information to js using the [self sendEventWithName:@"CustomEvent" body:@{@"name": @"111"}];

I agree this is a bit tricky and some help from the framework and documentation would be nice

This is also a very nice way. https://gist.github.com/brennanMKE/1ebba84a0fd7c2e8b481e4f8a5349b99 I think this is exactly what you are looking for?

Finally, I got the solution.

#import "RNNotification.h"
@implementation RNNotification

RCT_EXPORT_MODULE();

+ (id)allocWithZone:(NSZone *)zone {
    static RNNotification *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [super allocWithZone:zone];
    });
    return sharedInstance;
}

- (NSArray<NSString *> *)supportedEvents
{
    return @[@"EventReminder"];
}

- (void)sendNotificationToReactNative
{
    [self sendEventWithName:@"EventReminder" body:@{@"name": @"name"}];
}

When you use it!

RNNotification *notification = [RNNotification allocWithZone: nil];
[notification sendNotificationToReactNative]

Yep, singleton pattern is one option.

you save my day @Liqiankun
thank you!

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

@Liqiankun I did the same thing as you, still getting a damn error in my AppDelegate No visible @interface for 'RTNEventManager' declares the selector 'socketOpenConnection:userNumber:'

These are my files:

RTNEventManager.h

#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

@interface RTNEventManager : RCTEventEmitter <RCTBridgeModule>

@end

RTNEventManager.m

#import "RTNEventManager.h"

@implementation RTNEventManager

RCT_EXPORT_MODULE();

+ (id)allocWithZone:(NSZone *)zone {
    static RTNEventManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [super allocWithZone:zone];
    });
    return sharedInstance;
}

- (NSArray<NSString *> *)supportedEvents
{
  return @[@"onOpened"];
}

- (void)socketOpenConnection
{
  [self sendEventWithName:@"onOpened" body:@{@"callername": @"205-966-1916", @"username": @"9542493959"}];
}

@end

and then inside my AppDelegate.m I do this

#import "RTNEventManager.h"
...
RTNEventManager *manager = [RTNEventManager allocWithZone: nil];
[manager socketOpenConnection];

And when building it pops up the same error again and again.

expose an interface like this

'- (void) socketOpenConnection'

in your ".h" file

Was this page helpful?
0 / 5 - 0 ratings