Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Warning: Receiving warning from the console

Tags:

react-native

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`. 
like image 697
Hamid Hussainy Avatar asked Sep 11 '21 09:09

Hamid Hussainy


4 Answers

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()
 }
}, [])
like image 56
Qasim Zubair Avatar answered Oct 27 '22 13:10

Qasim Zubair


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.

like image 45
Karl Sander Avatar answered Oct 27 '22 13:10

Karl Sander


There is an upstream change, where in [email protected] 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.

like image 29
Kieran Osgood Avatar answered Oct 27 '22 15:10

Kieran Osgood


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

like image 4
Bharat Varma Avatar answered Oct 27 '22 13:10

Bharat Varma