Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created July 11, 2019 11:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sibelius/fc07bbe059da0cbe6ead211adc281f8f to your computer and use it in GitHub Desktop.
Save sibelius/fc07bbe059da0cbe6ead211adc281f8f to your computer and use it in GitHub Desktop.
NavigationPersistence snippet to save navigation state
<AppRouter
ref={stackNavigatorRef => NavigatorService.setContainer(stackNavigatorRef)}
screenProps={{
t,
theme,
}}
{...getPersistenceFunctions(navigationPersistenceKey)}
/>
import { AsyncStorage } from 'react-native';
const persistNavigationState = (persistenceKey: string) => async (navState) => {
try {
await AsyncStorage.setItem(persistenceKey, JSON.stringify(navState))
} catch(err) {
// handle the error according to your needs
}
};
const loadNavigationState = (persistenceKey: string) => async () => {
const jsonString = await AsyncStorage.getItem(persistenceKey)
return JSON.parse(jsonString)
};
export const getPersistenceFunctions = (persistenceKey: string) => {
if (!__DEV__) {
return undefined;
}
return {
persistNavigationState: persistNavigationState(persistenceKey),
loadNavigationState: loadNavigationState(persistenceKey),
};
};
@lucianomlima
Copy link

Change to import AsyncStorage from '@react-native-community/async-storage';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment