Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native. the close event of the application

On one of the pages of the developed application for ios on react native there is a switch components. When interacting with them, the state of the switches is stored in the Redux storage, but when leaving the page or leaving the application, you need to send the state data to the server. Leaving the page is implemented as follows:

props.navigation.addListener('willBlur', async() => {
  props.pushDataOnServer(data, token);
});

We add a navigation handler (StackNavigator) for the willBlur event, thus we can catch the transition to another page, but what about when closing the application directly from the edit page of the switches? Is there any event for this (e.g. willExit, willClose)? Or if you know a more effective way, please tell us

like image 786
Zimovik007 Avatar asked Dec 10 '22 06:12

Zimovik007


1 Answers

In your root component, you can watch for AppState changes.

componentDidMount() {
  AppState.addEventListener('change', this.handleAppStateChange);
}

componentWillUnmount() {
  AppState.removeEventListener('change', this.handleAppStateChange);
}

handleAppStateChange = (nextAppState) => {
  if (nextAppState === 'inactive') {
    console.log('the app is closed');
  }    
}
like image 186
vijayst Avatar answered Dec 24 '22 03:12

vijayst