From 039017bc2af69120d2d10e8f2c8a62919c37eb65 Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Sun, 10 May 2020 08:17:56 +0200 Subject: [PATCH] feat: initialState should take priority over deep link --- example/src/index.tsx | 13 +++++++------ packages/native/src/NavigationContainer.tsx | 12 ++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/example/src/index.tsx b/example/src/index.tsx index 023b0e78..45b458a1 100644 --- a/example/src/index.tsx +++ b/example/src/index.tsx @@ -7,6 +7,7 @@ import { I18nManager, Dimensions, ScaledSize, + Linking, } from 'react-native'; // eslint-disable-next-line import/no-unresolved import { enableScreens } from 'react-native-screens'; @@ -138,18 +139,18 @@ export default function App() { React.useEffect(() => { const restoreState = async () => { try { - let state; + const initialUrl = await Linking.getInitialURL(); - if (Platform.OS !== 'web' && state === undefined) { + if (Platform.OS !== 'web' || initialUrl === null) { const savedState = await AsyncStorage.getItem( NAVIGATION_PERSISTENCE_KEY ); - state = savedState ? JSON.parse(savedState) : undefined; - } + const state = savedState ? JSON.parse(savedState) : undefined; - if (state !== undefined) { - setInitialState(state); + if (state !== undefined) { + setInitialState(state); + } } } finally { try { diff --git a/packages/native/src/NavigationContainer.tsx b/packages/native/src/NavigationContainer.tsx index 0b321d1d..032fb7b8 100644 --- a/packages/native/src/NavigationContainer.tsx +++ b/packages/native/src/NavigationContainer.tsx @@ -22,7 +22,7 @@ type Props = NavigationContainerProps & { * Container component which holds the navigation state designed for React Native apps. * This should be rendered at the root wrapping the whole app. * - * @param props.initialState Initial state object for the navigation tree. When deep link handling is enabled, this will be ignored if there's an incoming link. + * @param props.initialState Initial state object for the navigation tree. When deep link handling is enabled, this will override deep links when specified. Make sure that you don't specify an `initialState` when there's a deep link (`Linking.getInitialURL()`). * @param props.onStateChange Callback which is called with the latest navigation state when it changes. * @param props.theme Theme object for the navigators. * @param props.linking Options for deep linking. Deep link handling is enabled when this prop is provided, unless `linking.enabled` is `false`. @@ -46,15 +46,13 @@ const NavigationContainer = React.forwardRef(function NavigationContainer( ...linking, }); - const [isReady, initialState = rest.initialState] = useThenable( - getInitialState - ); + const [isReady, initialState] = useThenable(getInitialState); React.useImperativeHandle(ref, () => refContainer.current); const linkingContext = React.useMemo(() => ({ options: linking }), [linking]); - if (isLinkingEnabled && !isReady) { + if (rest.initialState == null && isLinkingEnabled && !isReady) { // This is temporary until we have Suspense for data-fetching // Then the fallback will be handled by a parent `Suspense` component return fallback as React.ReactElement; @@ -65,7 +63,9 @@ const NavigationContainer = React.forwardRef(function NavigationContainer(