React Native - Global Event Listener

你离开我真会死。 提交于 2019-12-23 15:17:22

问题


I have a listener registered in my Pairing screen that calls a method whenever the connected Bluetooth device gets disconnected

// Pairing.js

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

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

componentWillUnmount() {
  this.handlerDisconnected.remove();
}

I want this event all across my app, how can I create a global event listener and not have to copy paste this code across every screen?

p.s I am using react-native-ble-manager and redux+sagas if that helps


回答1:


Add the listener in your top level component (App.js)



来源:https://stackoverflow.com/questions/52155306/react-native-global-event-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!