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
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');
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With