简体   繁体   English

反应本机 DeviceEventEmitter 取消订阅事件

[英]react native DeviceEventEmitter unsubscribe from event

I'm using DeviceEventEmitter to handle events of a favorite method, to which is subscribed in the constructor:我正在使用DeviceEventEmitter来处理在构造函数中订阅的最喜欢方法的事件:

DeviceEventEmitter.addListener("FavoriteClick", async (e) => 
{
    // do something
})

This event listener stays active whenever the components unmounts (permenantly).只要组件卸载(永久),此事件侦听器就会保持活动状态。 What do I have to call to unsub?我必须调用什么来取消订阅? I've tried storing the event as a variable and calling listener.removeCurrentListener() in the componentWillUnmount() like the (limited) documentation states , if I understand that correctly, but removeCurrentListener() is not a method.我已经尝试将事件存储为变量并在componentWillUnmount()中调用listener.removeCurrentListener()就像(有限的)文档状态一样,如果我理解正确的话,但removeCurrentListener()不是一种方法。

DeviceEventEmitter is deprecated , you should use NativeEventEmitter instead. DeviceEventEmitter弃用,您应该改用NativeEventEmitter

Example : 示例

import { NativeEventEmitter, NativeModules } from 'react-native';

const { CalendarManager } = NativeModules;

const calendarManagerEmitter = new NativeEventEmitter(CalendarManager);

const subscription = calendarManagerEmitter.addListener(
  'EventReminder',
  (reminder) => console.log(reminder.name)
);

...

// Don't forget to unsubscribe, typically in componentWillUnmount
subscription.remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native DeviceEventEmitter keyboardWillShow停止工作 - React Native DeviceEventEmitter keyboardWillShow stopped working 取消订阅事件监听器反应钩子 - Unsubscribe from event listener react hooks 如何从 React Native Expo 推送通知取消订阅(删除监听器) - How to unsubscribe(remove a listener) from React Native Expo push notifications 如何取消订阅 React-Native 中的 Firestore 侦听器? - How do I unsubscribe from a Firestore listener in React-Native? 问题设置取消订阅 react-native-location 到 state 变量 - Issue setting unsubscribe from react-native-location to state variable 订阅和取消订阅 React Native Netinfo 的正确方法 - Correct way to subscribe and unsubscribe React Native Netinfo React Native unsubscribe.current 不是 function - React Native unsubscribe.current is not a function 反应 如何在React componentWillUnmount中取消订阅事件处理程序? - React. How to unsubscribe event handlers in React componentWillUnmount? React Native Firebase onStateChanged 在卸载时不会取消订阅 - React Native Firebase onStateChanged doesn't unsubscribe on unmount React Native 上的 DeviceOrientation 事件 - DeviceOrientation Event on React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM