21

I'm new in react native and I am receiving this warning or error when I run my react native project.

           ReactNativeJS ▶︎ 'EventEmitter.removeListener(\'change\', ...): Method has
           been deprecated. Please instead use `remove()` on the subscription returned by 
          `EventEmitter.addListener`. 
Hamid Hussainy
  • 358
  • 1
  • 5
  • 11
  • Does [this](https://stackoverflow.com/questions/36886628/how-do-you-remove-a-listener-from-react-natives-eventemitter-instance) answer your question? – Rohit Aggarwal Sep 11 '21 at 09:34

5 Answers5

17

Once you are sure that the deprecated use is happening in a dependency you cannot control, it is possible to silence these warnings. In your App.js or somewhere else add:

import { LogBox } from "react-native";

LogBox.ignoreLogs(["EventEmitter.removeListener"]);

You have to use this with care, and probably remove it at some point in the future, because it will silence all warnings that contain EventEmitter.removeListener even ones that may be important to you.

Karl Sander
  • 171
  • 1
  • 2
  • This for me removed simple the warning in the actual react-native app not in console but resolved the situation my redux action was prevent to be handled. – Carmine Tambascia Aug 09 '22 at 13:14
11

There is an upstream change, where in react-native@0.65.0 they deprecated the old api in favour of a more terse name, so removeListener() becomes remove().

This means that any package you use that calls these will produce warnings as such, until they release new package versions to address this.

Your comment to Bharat Varma seems to suggest you're using https://github.com/react-native-community - they haven't released a new version in a long time, and there are open Pull Requests targeting this same issue (which are going unaddressed at the moment).

Your best bet to get rid of it for now would be to use patch-package to modify the node module with the PR fix linked above (just need to change removeListener to remove on the useDeviceOrientation hook they export), and use this until they merge fixes for this.

Kieran Osgood
  • 918
  • 6
  • 17
  • One year later, the package is not been updated yet. The best thing is to create your own hook for device orientation management. – tezqa Dec 30 '21 at 08:39
10

According to official document, as of React Native Version 0.65+, removeListener is depreciated. You can use remove() now:

    useEffect(() => {
 const subscription = AppState.addEvenListener('change', ()=>{})
 return () => {
   subscription.remove()
 }
}, [])
Qasim Zubair
  • 177
  • 2
  • 3
4

here’s how you remove the event listener.

const susbcription = EventEmitter.addListener(‘change’,some_callback_function)

to remove the listener. isntead of EventEmitter.removeListener. u do this

subscription.remove()

Bharat Varma
  • 154
  • 1
  • 6
0

You can use this approach to solve your problem while coding. Change in library code adding patches is not the great option.

var eventSubscription; 
    useEffect(() => {
        getDarkOrLightMode();
        eventSubscription =  Appearance.addChangeListener(onThemeChange);
        console.log('useEffect in DarkOrLight',eventSubscription);
        return () => {
          console.log("before   ---->  ",  eventSubscription );
          eventSubscription.remove();
          console.log("after   ---->  ",  eventSubscription );
        };
      }, []);