Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.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.

like image 825
Sander Koldenhof Avatar asked Sep 05 '26 01:09

Sander Koldenhof


1 Answers

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();
like image 125
Orelsanpls Avatar answered Sep 06 '26 16:09

Orelsanpls



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!