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.

1

1 Answer

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like