-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(boilerplate): fix deep link not working with navigation persist #2679
Conversation
@vanenshi Thanks for the contribution! Can you supply steps to reproduce / test this change for us so we can confirm the fix? |
Hi @frankcalise
Expected Behavioropen the app and go to the Actual behaviorapp stays on the persisted navigation status (last page that was open) NoteI picked up the fix from the react-navigation website, here is their example code, pay attention to the import * as React from 'react';
import { Linking, Platform } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { NavigationContainer } from '@react-navigation/native';
const PERSISTENCE_KEY = 'NAVIGATION_STATE_V1';
export default function App() {
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 (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;
if (state !== undefined) {
setInitialState(state);
}
}
} finally {
setIsReady(true);
}
};
if (!isReady) {
restoreState();
}
}, [isReady]);
if (!isReady) {
return null;
}
return (
<NavigationContainer
initialState={initialState}
onStateChange={(state) =>
AsyncStorage.setItem(PERSISTENCE_KEY, JSON.stringify(state))
}
>
{/* ... */}
</NavigationContainer>
);
} |
any update on this? we have the same problem |
@mustafahsz later this week probably, but feel free to just incorporate the code into your project |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution and the provided documentation, left one question for you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
# [9.7.0](v9.6.3...v9.7.0) (2024-05-29) ### Bug Fixes * **boilerplate:** fix deep link not working with navigation persist ([#2679](#2679) by [@vanenshi](https://github.com/vanenshi)) ([a0cd8ce](a0cd8ce)) * **cli:** better support for yarn 2+ ([#2684](#2684) by [@frankcalise](https://github.com/frankcalise) and [@morganick](https://github.com/morganick)) ([e38a02c](e38a02c)) * **metro:** Add support for cjs extension in metro.config.js ([#2689](#2689) by [@jamonholmgren](https://github.com/jamonholmgren)) ([7544fac](7544fac)) ### Features * **boilerplate:** upgrade to Expo SDK 51 ([#2683](#2683) by [@frankcalise](https://github.com/frankcalise)) ([e0f3aa8](e0f3aa8)), closes [#2692](#2692)
🎉 This PR is included in version 9.7.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Please verify the following:
yarn test
jest tests pass with new tests, if relevantyarn lint
eslint checks pass with new code, if relevantyarn format:check
prettier checks pass with new code, if relevantREADME.md
(or relevant documentation) has been updated with your changesDescribe your PR
On the dev env, if you try to start the app from a deep link because the navigation persists, it will get ignored by the navigation