React Native: is there a callback function when your application is closed?

I have setInterval that continues to work even when you close (don't close) the application. I would like to call a function when my application is closed or the device goes to sleep so that it clears setInterval.

+7
javascript react-native
source share
2 answers

AppState is your friend! See the AppState documentation .

So, in your component where setTimeout exists, you just need an AppState and add an event listener as follows:

AppState.addEventListener('background', this.handlePutAppToBackground); AppState.addEventListener('inactive', this.handlePutAppToBackground); 

handlePutAppToBackground () will now be a method in your component where you would call clearTimeout (...)

+5
source share

itinance was close. The answer actually is to use

 AppState.addEventListener('change', state => { if (state === 'active') { // do this } else if (state === 'background') { // do that } else if (state === 'inactive') { // do that other thing } }); 
+2
source share

All Articles