react native. the close event of the application

断了今生、忘了曾经 提交于 2020-05-26 09:34:25

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/51182419/react-native-the-close-event-of-the-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!