mirror of
https://github.com/zoriya/react-native-svg.git
synced 2025-12-05 22:56:11 +00:00
# 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.
51 lines
1.5 KiB
TypeScript
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};
|
|
};
|