RCTEventEmitter bridge is not set

See original GitHub issue

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
  • Development Operating System:macOS
  • Build tools:iOS 10

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

28reactions
Liqiankuncommented, Oct 10, 2017

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]
2reactions
ogil7190commented, Sep 8, 2018

expose an interface like this

‘- (void) socketOpenConnection’

in your “.h” file

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implement RCTEventEmitter in Swift but receive exception for ...
Bridge is not set. This is probably because you've explicitly synthesized the bridge in MyEventEmitter, even though it's inherited from ...
Read more >
Emitting (and listening for) events - Building React Native ...
We need to have an empty init constructor; no need to put anything in here. We then create a function registerEmitter to pass...
Read more >
RN中OC给JS发消息崩溃报错:bridge is not set. This is ... - 简书
该崩溃报错是由于写法不规范导致的一、官方文档和不合理想法1.官方文档2.不合理想法由于该文档只是点明了calendarEventReminderReceived方法传...
Read more >
Implement RCTEventEmitter in Swift but receive exception for ...
Coding example for the question Implement RCTEventEmitter in Swift but receive exception for bridge is not set-swift.
Read more >
Native iOS Module in React Native using Swift - YouTube
Learn How to create a Native Module in iOS using swift. Complete Courses: React Native: https://bit.ly/3tDRMTdFlutter: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found