You might want to save the user's location in the app, so that they are immediately returned to the same location after the app is restarted.
This is especially valuable during development because it allows the developer to stay on the same screen when they refresh the app.
Note: This feature is currently considered experimental, because of the warnings listed at the end of this page. Use with caution!
To be able to persist the navigation state, we can use the
initialState props of the container.
onStateChange- This prop notifies us of any state changes. We can persist the state in this callback.
initialState- This prop allows us to pass an initial state to use for navigation state. We can pass the restored state in this prop.
This feature is particularly useful in development mode. You can enable it selectively using the following approach:
Because the state is restored asynchronously, the app must render an empty/loading view for a moment before we have the initial state. To handle this, we can return a loading view when
Warning: Serializable State
Each param, route, and navigation state must be fully serializable for this feature to work. Typically, you would serialize the state as a JSON string. This means that your routes and params must contain no functions, class instances, or recursive data structures.
You can modify the initial state object before passing it to container, but note that if your
initialState provides an invalid object (an object from which the navigation state cannot be recovered), React Navigation may not be able to handle the situation gracefully.