react-nativereact-navigation-v5

React Navigation - initial prop when opening via deeplink url


As stated in the react-navigation V5.x docs: "If you need to render the initial route specified in the navigator, you can disable the behaviour of using the specified screen as the initial screen by setting initial: false:"

navigation.navigate('Root', { screen: 'Settings', initial: false, });

So, are there any best practices for handling a url like myapp://root/settings with a path in such a case.. let's say this url is opening your app via push or some other external source. URL is handled with Linking.openURL(url).catch();

What if the Settings screen is not the root/initial screen of the stack, but maybe Feed-> So the back button on Settings will direct somewhere else (or nowhere).

Is there a better approach besides hacking this inside onReceiveURL() with checking if the initial screen exists on the Stack? Am I conceptually completely missing something here?

** imo: there should also be a better approach for this bedsides using the lazy={false} prop


UPDATE:

You'll need to set an initialRouteName not only on the stack navigator itself but also accordingly on your Linking config.. this was missing for me:

https://reactnavigation.org/docs/5.x/configuring-links#rendering-an-initial-route


Solution

  • You'll need to set an initialRouteName not only on the stack navigator itself but also accordingly on your Linking config.. this was missing for me:

    https://reactnavigation.org/docs/5.x/configuring-links#rendering-an-initial-route