Saturday, May 11, 2024
47
rated 0 times [  48] [ 1]  / answers: 1 / hits: 16984  / 5 Years ago, tue, october 8, 2019, 12:00:00

I'm using DeviceEventEmitter to handle events of a favorite method, to which is subscribed in the constructor:



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.


More From » react-native

 Answers
13

DeviceEventEmitter is deprecated, you should use NativeEventEmitter instead.



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

[#51594] Thursday, September 26, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stefanicarolinat

Total Points: 145
Total Questions: 91
Total Answers: 93

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
stefanicarolinat questions
Mon, Nov 15, 21, 00:00, 3 Years ago
Fri, Apr 16, 21, 00:00, 3 Years ago
Thu, Oct 15, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
;