React Navigation – 通过深度链接 URL 打开时的初始属性

huangapple go评论61阅读模式
英文:

React Navigation - initial prop when opening via deeplink url

问题

根据react-navigation V5.x文档的说明:“如果您需要呈现导航器中指定的初始路由,可以通过设置initial: false来禁用使用指定的屏幕作为初始屏幕的行为:”

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

那么,有没有处理类似myapp://root/settings这样带有路径的URL的最佳实践呢?假设这个URL是通过推送或其他外部来源打开您的应用程序的。可以使用Linking.openURL(url).catch();来处理URL。

如果Settings屏幕不是堆栈的根/初始屏幕,而是Feed->,那么Settings上的返回按钮将指向其他地方(或者根本不指向任何地方)。

除了在onReceiveURL()中进行检查初始屏幕是否存在于堆栈中的方法之外,是否还有更好的方法?我是否在概念上完全错过了什么?

**在我看来,除了使用lazy={false}属性之外,还应该有更好的方法来解决这个问题。


更新:

您需要不仅在堆栈导航器本身上设置initialRouteName,还需要相应地在您的链接配置中设置它。这一点我之前忽略了:

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

英文:

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

答案1

得分: 0

你需要在堆栈导航器本身以及相应的链接配置中设置一个初始路由名称... 这在我的情况下缺失了:

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

英文:

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

huangapple
  • 本文由 发表于 2023年6月21日 23:07:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定