React Native - 全局事件监听器

And*_*aci 6 bluetooth reactjs react-native react-native-ble-plx

我在我的Pairing屏幕上注册了一个监听器,它在连接的蓝牙设备断开连接时调用一个方法

// Pairing.js

const BleManagerModule = NativeModules.BleManager;
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule);

componentDidMount() {
  this.handlerDisconnected = bleManagerEmitter.addListener(
    "BleManagerDisconnectPeripheral",
    this.handlePeripheralDisconnected
  );
}

componentWillUnmount() {
  this.handlerDisconnected.remove();
}
Run Code Online (Sandbox Code Playgroud)

我希望这个事件贯穿我的应用程序,如何创建全局事件侦听器而不必在每个屏幕上复制粘贴此代码?

ps 如果有帮助,我正在使用 react-native-ble-manager 和 redux+saga

bas*_*ase 1

在顶级组件 (App.js) 中添加监听器