【问题标题】:Is react native modules sending events to javascript doc correct?将事件发送到 javascript 文档的反应本机模块是否正确?
【发布时间】:2015-06-08 02:44:24
【问题描述】:

在 facebook 的 doc 上写着

- (void)calendarEventReminderReceived:(NSNotification *)notification
{
    NSString *eventName = notification.userInfo[@"name"];
    [self.bridge.eventDispatcher sendAppEventWithName:@"EventReminder"
                                               body:@{@"name": eventName}];
}

JavaScript 代码可以订阅这些事件:

var subscription = DeviceEventEmitter.addListener(
    'EventReminder',
    (reminder) => console.log(reminder.name)
);

但我永远无法完成这项工作。这应该是 sendDeviceEventWithName 吗?原生模块也应该总是单例的吗?

【问题讨论】:

    标签: javascript ios objective-c facebook react-native


    【解决方案1】:

    目前,没有。请看这个问题:

    https://github.com/facebook/react-native/issues/394

    将示例 Objective-C 更改为调用 sendDeviceEventWithName 而不是 sendAppEventWithName 可以解决问题。

    这似乎证实了您的建议。有一个拉取请求等待合并:

    https://github.com/facebook/react-native/pull/530

    【讨论】:

    • 酷,谢谢。更改为 sendDeviceEventWithName 后,它似乎工作正常。
    • 我自己真的无法让它工作!您有机会在某处发布您的(简化的)代码吗?
    • 诀窍是让你的 natvie 模块成为单例。只需在您的实例中检查 _bridge 并确保它不为零。我不确定这是一个功能还是一个错误。
    • 是的,当我尝试时 _bridge 是 nil - 你如何让它成为一个单例?
    • @implementation NativeEventBridge @synthesize bridge = _bridge; + (id)allocWithZone:(NSZone *)zone { static NativeEventBridge *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [super allocWithZone:zone]; }); return sharedInstance; } @end
    猜你喜欢
    • 2018-11-25
    • 2015-10-30
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    相关资源
    最近更新 更多