How do I save state in React Native only if the app closes? In android development, a part of an app’s lifecycle is onStop, which permits you the ability to save state once the app is closed. I’m only really talking if the user closes the app on accident, I’d want the current state to be saved to a flat file. I have a backend with Postgres that the app communicates to.
I apologize if there is an obvious answer, currently learning react native.
2
Answers
Thanks for asking. It depends on your requirements if you want to store the app state in the local storage or in your database. I faced a similar requirement and I used react-native-async-storage and AppState API from react-native library.
The steps are:
AppState
API to add the event listener for the change in the state of app.If you face an issue like a crash or something for the app closes, then you can use local storage for saving states like I have used [react-native-async-storage][1] and [realm-react-native][2].