只能让react-native事件与不推荐使用的'sendAppEventWithName'函数

时间:2017-01-09 21:53:55

标签: events react-native

我正在尝试在截屏时向我的应用发送一个事件。我使用不推荐使用的eventDispatcher sendAppEventWithName进行了此操作。我正在尝试使用RCTEventEmitter sendWithName,但它不起作用,我不知道为什么。这是我在本机中的功能,我已经确认在截取屏幕截图时会调用它:

- (void)screenshotEventReceived:(NSNotification *)notification {
  [self sendEventWithName:@"ScreenshotTaken" body:@"hello"];
}

听取的JS:

componentDidMount() {
    this.eventEmitter = new NativeEventEmitter(NativeModules.ScreenshotManager);
    this.eventEmitter.addListener('ScreenshotTaken', () => {
      console.log('WE GOT AN EVENT!!!');
    }, {});
}

如果我用sendEventWithName替换Objective-C中的[self.bridge.eventDispatcher sendAppEventWithName:@"ScreenshotTaken" body:@"hello"];调用,那么这样可以正常工作。我不想这样做,因为它已被弃用,但不明白为什么不推荐的方式不起作用。当我截取屏幕截图时,我收到黄色方框并发出警告说“发送'截图'没有听众注册”。

0 个答案:

没有答案