diff --git a/versioned_docs/version-7.x/state-persistence.md b/versioned_docs/version-7.x/state-persistence.md
index 8d6c230867b..b59644962c0 100755
--- a/versioned_docs/version-7.x/state-persistence.md
+++ b/versioned_docs/version-7.x/state-persistence.md
@@ -4,6 +4,9 @@ title: State persistence
sidebar_label: State persistence
---
+import Tabs from '@theme/Tabs';
+import TabItem from '@theme/TabItem';
+
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.
@@ -15,13 +18,192 @@ To be able to persist the [navigation state](navigation-state.md), we can use th
- `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](navigation-state.md). We can pass the restored state in this prop.
-
+
+
-```js
+```js name="Persisting the navigation state" snack version=7 dependencies=@react-native-async-storage/async-storage
import * as React from 'react';
-import { Linking, Platform } from 'react-native';
+// codeblock-focus-start
+import { Platform, View, Linking } from 'react-native';
+import AsyncStorage from '@react-native-async-storage/async-storage';
+import {
+ useNavigation,
+ createStaticNavigation,
+} from '@react-navigation/native';
+// codeblock-focus-end
+import { Button } from '@react-navigation/elements';
+import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
+import { createNativeStackNavigator } from '@react-navigation/native-stack';
+
+function A() {
+ return ;
+}
+
+function B() {
+ const navigation = useNavigation();
+
+ return (
+
+
+
+ );
+}
+
+function C() {
+ const navigation = useNavigation();
+
+ return (
+
+
+
+ );
+}
+
+function D() {
+ return ;
+}
+
+const HomeStackScreen = createNativeStackNavigator({
+ screens: {
+ A: A,
+ },
+});
+
+const SettingsStackScreen = createNativeStackNavigator({
+ screens: {
+ B: B,
+ C: C,
+ D: D,
+ },
+});
+
+// codeblock-focus-start
+
+const PERSISTENCE_KEY = 'NAVIGATION_STATE_V1';
+
+export default function App() {
+ const [isReady, setIsReady] = React.useState(Platform.OS === 'web'); // Don't persist state on web since it's based on URL
+ const [initialState, setInitialState] = React.useState();
+
+ React.useEffect(() => {
+ const restoreState = async () => {
+ try {
+ const initialUrl = await Linking.getInitialURL();
+
+ if (Platform.OS !== 'web' && initialUrl == null) {
+ const savedState = await AsyncStorage.getItem(PERSISTENCE_KEY);
+ const state = savedState ? JSON.parse(savedState) : undefined;
+
+ if (state !== undefined) {
+ setInitialState(state);
+ }
+ }
+ } finally {
+ setIsReady(true);
+ }
+ };
+
+ if (!isReady) {
+ restoreState();
+ }
+ }, [isReady]);
+
+ if (!isReady) {
+ return null;
+ }
+ const Tab = createBottomTabNavigator({
+ screens: {
+ Home: {
+ screen: HomeStackScreen,
+ options: {
+ headerShown: false,
+ tabBarLabel: 'Home!',
+ },
+ },
+ Settings: {
+ screen: SettingsStackScreen,
+ options: {
+ headerShown: false,
+ tabBarLabel: 'Settings!',
+ },
+ },
+ },
+ });
+ const Navigation = createStaticNavigation(Tab);
+
+ return (
+
+ AsyncStorage.setItem(PERSISTENCE_KEY, JSON.stringify(state))
+ }
+ />
+ );
+}
+// codeblock-focus-end
+```
+
+
+
+
+```js name="Persisting the navigation state" snack version=7 dependencies=@react-native-async-storage/async-storage
+import * as React from 'react';
+// codeblock-focus-start
+import { Platform, View, Linking } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { NavigationContainer } from '@react-navigation/native';
+// codeblock-focus-end
+import { Button } from '@react-navigation/elements';
+import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
+import { createNativeStackNavigator } from '@react-navigation/native-stack';
+
+const Tab = createBottomTabNavigator();
+const HomeStack = createNativeStackNavigator();
+const SettingsStack = createNativeStackNavigator();
+
+function A() {
+ return ;
+}
+
+function B({ navigation }) {
+ return (
+
+
+
+ );
+}
+
+function C({ navigation }) {
+ return (
+
+
+
+ );
+}
+
+function D() {
+ return ;
+}
+
+function HomeStackScreen() {
+ return (
+
+
+
+ );
+}
+
+function SettingsStackScreen() {
+ return (
+
+
+
+
+
+ );
+}
+
+// codeblock-focus-start
const PERSISTENCE_KEY = 'NAVIGATION_STATE_V1';
@@ -66,12 +248,27 @@ export default function App() {
AsyncStorage.setItem(PERSISTENCE_KEY, JSON.stringify(state))
}
>
- {/* ... */}
+
+
+
+
);
}
+// codeblock-focus-end
```
+
+
+
### Development Mode
This feature is particularly useful in development mode. You can enable it selectively using the following approach: