react-native-countdown-component icon indicating copy to clipboard operation
react-native-countdown-component copied to clipboard

Deprecated AppState.removeEventListener on react v0.70

Open binotby opened this issue 3 years ago • 9 comments

Simulator Screen Shot - iPhone 13 - 2022-10-13 at 00 58 21

binotby avatar Oct 12 '22 18:10 binotby

In the meantime, a fix to this is editing the index.js file as follows:

`state = { until: Math.max(this.props.until, 0), lastUntil: null, wentBackgroundAt: null, eventListener: null, // NEW LINE }; // ... componentDidMount() { this.state.eventListener = AppState.addEventListener('change', this._handleAppStateChange); // MODIFIED }

componentWillUnmount() { clearInterval(this.timer); this.state.eventListener.remove(); // MODIFIED }`

ullmanj avatar Nov 30 '22 01:11 ullmanj

@ullmanj , i don't have this content in index.ts file (Using typescript). I can only see the interfaces mentioned in index.ts file under react-native-countdown-component inside node_modules. Can you help?

Swarnim01 avatar Dec 07 '22 17:12 Swarnim01

Hi! Yes — I found the code above in the index file under react-native-countdown-component inside node_modules. I see that binotby linked a fix above. Maybe you could try that if the code isn't showing up for you in the index file?

ullmanj avatar Dec 14 '22 22:12 ullmanj

Thanks @ullmanj that worked as expected

Here you can find the patch-package react-native-countdown-component+2.7.1.patch

diff --git a/node_modules/react-native-countdown-component/index.js b/node_modules/react-native-countdown-component/index.js
index b546b82..29971f8 100644
--- a/node_modules/react-native-countdown-component/index.js
+++ b/node_modules/react-native-countdown-component/index.js
@@ -43,6 +43,7 @@ class CountDown extends React.Component {
     until: Math.max(this.props.until, 0),
     lastUntil: null,
     wentBackgroundAt: null,
+    eventListener: null, // NEW LINE
   };
 
   constructor(props) {
@@ -51,12 +52,13 @@ class CountDown extends React.Component {
   }
 
   componentDidMount() {
-    AppState.addEventListener('change', this._handleAppStateChange);
+    this.state.eventListener = AppState.addEventListener('change', this._handleAppStateChange); // MODIFIED
+
   }
 
   componentWillUnmount() {
     clearInterval(this.timer);
-    AppState.removeEventListener('change', this._handleAppStateChange);
+    this.state.eventListener.remove(); // MODIFIED
   }
 
   componentDidUpdate(prevProps, prevState) {

jonra1993 avatar Jan 09 '23 18:01 jonra1993

In the meantime, a fix to this is editing the index.js file as follows:

`state = { until: Math.max(this.props.until, 0), lastUntil: null, wentBackgroundAt: null, eventListener: null, // NEW LINE }; // ... componentDidMount() { this.state.eventListener = AppState.addEventListener('change', this._handleAppStateChange); // MODIFIED }

componentWillUnmount() { clearInterval(this.timer); this.state.eventListener.remove(); // MODIFIED }`

This is working thanks..........................................................................................

zahoormohammed avatar Jan 17 '23 07:01 zahoormohammed

     until: Math.max(this.props.until, 0),
     lastUntil: null,
     wentBackgroundAt: null,
+    eventListener: null, // NEW LINE
   };

 
   componentDidMount() {
-    AppState.addEventListener('change', this._handleAppStateChange);
+    this.state.eventListener = AppState.addEventListener('change', this._handleAppStateChange); // MODIFIED
+
   }
 
   componentWillUnmount() {
     clearInterval(this.timer);
-    AppState.removeEventListener('change', this._handleAppStateChange);
+    this.state.eventListener.remove(); // MODIFIED
   }
 
   componentDidUpdate(prevProps, prevState) {

Andres6936 avatar Jan 30 '23 20:01 Andres6936

getting this error while using the above code undefined is not an object (evaluating 'this.state.eventListner.remove' ) Please help "react-native-countdown-component": "^2.7.1",

rohit-k-singh avatar May 31 '23 05:05 rohit-k-singh

@Rksingh620 It seems that there is a typo: you wrote "eventListner" when it should be "eventListener" based on the code above. I hope that helps!

ullmanj avatar Aug 06 '23 00:08 ullmanj

To fix this error, open node modules and find the react-native-countdown-component folder, open the folder and open the index.js file.

find the componentDidMount() function and replace it with this code

componentDidMount() {
  this.subscription = AppState.addEventListener('change', this._handleAppStateChange);
 }

then find the componentWillUnmount() function and replace it with

componentWillUnmount() {
    clearInterval(this.timer);
    this.subscription.remove();
  }

ksowah avatar Oct 19 '23 20:10 ksowah