英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论