Files
react-native-svg/apps/common/example/utils/usePersistNavigation.ts
Jakub Grzywacz b3b175a7fb feat: move examples to ./apps (#2507)
# Summary

Due to the large number of example apps in the repository, I decided to
change the structure and move all applications into an "apps" folder to
maintain a clear structure.
2024-10-25 16:12:23 +02:00

51 lines
1.5 KiB
TypeScript

import AsyncStorage from '@react-native-async-storage/async-storage';
import {NavigationState} from '@react-navigation/native';
import {useCallback, useEffect, useState} from 'react';
import {Linking, Platform} from 'react-native';
function noop() {} // do nothing
// copied from https://reactnavigation.org/docs/state-persistence/
const PERSISTENCE_KEY = 'NAVIGATION_STATE_V1';
export const usePersistNavigation = () => {
const [isReady, setIsReady] = useState(!__DEV__);
const [initialState, setInitialState] = useState();
useEffect(() => {
const restoreState = async () => {
try {
const initialUrl = await Linking.getInitialURL();
if (
Platform.OS !== 'web' &&
Platform.OS !== 'macos' &&
initialUrl == null
) {
// Only restore state if there's no deep link and we're not on web
const savedStateString = await AsyncStorage.getItem(PERSISTENCE_KEY);
const state = savedStateString
? JSON.parse(savedStateString)
: undefined;
if (state !== undefined) {
setInitialState(state);
}
}
} finally {
setIsReady(true);
}
};
if (!isReady) {
restoreState().catch(noop);
}
}, [isReady]);
const persistNavigationState = useCallback((state?: NavigationState) => {
AsyncStorage.setItem(PERSISTENCE_KEY, JSON.stringify(state)).catch(noop);
}, []);
return {isReady, initialState, persistNavigationState};
};