Skip to content

Commit

Permalink
Tweak state persistence snippet for web
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jan 23, 2024
1 parent 66a6c23 commit c799fe5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions versioned_docs/version-6.x/state-persistence.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ import { NavigationContainer } from '@react-navigation/native';
const PERSISTENCE_KEY = 'NAVIGATION_STATE_V1';

export default function App() {
const [isReady, setIsReady] = React.useState(false);
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) {
// Only restore state if there's no deep link and we're not on web
if (initialUrl == null) {
// Only restore state if there's no deep link
const savedStateString = await AsyncStorage.getItem(PERSISTENCE_KEY);
const state = savedStateString ? JSON.parse(savedStateString) : undefined;
const state = savedStateString
? JSON.parse(savedStateString)
: undefined;

if (state !== undefined) {
setInitialState(state);
Expand Down
10 changes: 6 additions & 4 deletions versioned_docs/version-7.x/state-persistence.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ import { NavigationContainer } from '@react-navigation/native';
const PERSISTENCE_KEY = 'NAVIGATION_STATE_V1';

export default function App() {
const [isReady, setIsReady] = React.useState(false);
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) {
// Only restore state if there's no deep link and we're not on web
if (initialUrl == null) {
// Only restore state if there's no deep link
const savedStateString = await AsyncStorage.getItem(PERSISTENCE_KEY);
const state = savedStateString ? JSON.parse(savedStateString) : undefined;
const state = savedStateString
? JSON.parse(savedStateString)
: undefined;

if (state !== undefined) {
setInitialState(state);
Expand Down

0 comments on commit c799fe5

Please sign in to comment.