Skip to content

Commit c799fe5

Browse files
committed
Tweak state persistence snippet for web
1 parent 66a6c23 commit c799fe5

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

versioned_docs/version-6.x/state-persistence.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@ import { NavigationContainer } from '@react-navigation/native';
2626
const PERSISTENCE_KEY = 'NAVIGATION_STATE_V1';
2727

2828
export default function App() {
29-
const [isReady, setIsReady] = React.useState(false);
29+
const [isReady, setIsReady] = React.useState(Platform.OS === 'web'); // Don't persist state on web since it's based on URL
3030
const [initialState, setInitialState] = React.useState();
3131

3232
React.useEffect(() => {
3333
const restoreState = async () => {
3434
try {
3535
const initialUrl = await Linking.getInitialURL();
3636

37-
if (Platform.OS !== 'web' && initialUrl == null) {
38-
// Only restore state if there's no deep link and we're not on web
37+
if (initialUrl == null) {
38+
// Only restore state if there's no deep link
3939
const savedStateString = await AsyncStorage.getItem(PERSISTENCE_KEY);
40-
const state = savedStateString ? JSON.parse(savedStateString) : undefined;
40+
const state = savedStateString
41+
? JSON.parse(savedStateString)
42+
: undefined;
4143

4244
if (state !== undefined) {
4345
setInitialState(state);

versioned_docs/version-7.x/state-persistence.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@ import { NavigationContainer } from '@react-navigation/native';
2626
const PERSISTENCE_KEY = 'NAVIGATION_STATE_V1';
2727

2828
export default function App() {
29-
const [isReady, setIsReady] = React.useState(false);
29+
const [isReady, setIsReady] = React.useState(Platform.OS === 'web'); // Don't persist state on web since it's based on URL
3030
const [initialState, setInitialState] = React.useState();
3131

3232
React.useEffect(() => {
3333
const restoreState = async () => {
3434
try {
3535
const initialUrl = await Linking.getInitialURL();
3636

37-
if (Platform.OS !== 'web' && initialUrl == null) {
38-
// Only restore state if there's no deep link and we're not on web
37+
if (initialUrl == null) {
38+
// Only restore state if there's no deep link
3939
const savedStateString = await AsyncStorage.getItem(PERSISTENCE_KEY);
40-
const state = savedStateString ? JSON.parse(savedStateString) : undefined;
40+
const state = savedStateString
41+
? JSON.parse(savedStateString)
42+
: undefined;
4143

4244
if (state !== undefined) {
4345
setInitialState(state);

0 commit comments

Comments
 (0)