React Native-全局事件监听器 [英] React Native - Global Event Listener

查看:1330
本文介绍了React Native-全局事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Pairing屏幕上注册了一个侦听器,每当连接的蓝牙设备断开连接时,该侦听器都会调用一个方法

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();
}

我希望整个应用程序都具有此事件,如何创建一个 global 事件监听器,而不必在每个屏幕上复制粘贴此代码?

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如果有帮助,我正在使用react-native-ble-manager和redux + sagas

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

推荐答案

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

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

这篇关于React Native-全局事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆