[英]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
。
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.