Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecated AppState.removeEventListener on react v0.70 #113

Open
binotby opened this issue Oct 12, 2022 · 9 comments · May be fixed by #114
Open

Deprecated AppState.removeEventListener on react v0.70 #113

binotby opened this issue Oct 12, 2022 · 9 comments · May be fixed by #114

Comments

@binotby
Copy link

binotby commented Oct 12, 2022

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

@binotby binotby linked a pull request Oct 12, 2022 that will close this issue
1 task
@ullmanj
Copy link

ullmanj commented Nov 30, 2022

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
}`

@Swarnim01
Copy link

Swarnim01 commented Dec 7, 2022

@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?

@ullmanj
Copy link

ullmanj commented Dec 14, 2022

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?

@jonra1993
Copy link

jonra1993 commented Jan 9, 2023

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

@zahoormohammed
Copy link

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

@Andres6936
Copy link

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

@rohit-k-singh
Copy link

rohit-k-singh commented May 31, 2023

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",

@ullmanj
Copy link

ullmanj commented Aug 6, 2023

@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!

@ksowah
Copy link

ksowah commented Oct 19, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants