React Navigation V2:navigation.push和navigation.navigate之间的区别

问题描述 投票:6回答:2

我是React Native的新手,我正在研究React Native Navigation Docs。我想知道:navigation.push()navigation.navigate()有什么区别?

我试着自己找出来,但他们似乎完成了同样的事情......

reactjs react-native react-navigation
2个回答
12
投票

如果你查看push的文档,可以解释它们有多么不同。

Push操作在堆栈顶部添加一个路径并向前导航到它。这与导航不同,如果组件已经安装在那里,导航将弹回到堆栈中的早期。推送将始终添加到顶部,因此可以多次安装组件。

我们可以以Instagram为例;

考虑导航到用户的个人资料。然后,您可以检查用户的关注者,然后您也可以导航到他们的个人资料。如果您仅使用navigate操作执行相同的操作,当您从关注者列表屏幕单击用户的配置文件时将导航到之前的配置文件,但如果您使用push它将推送新的屏幕到堆栈并显示正确的配置文件。这样你可以goBack到第一个屏幕。


3
投票

根据上一篇博文here:for v1:

navigate(routeName) and push(routeName) were very similar: every time you called navigate(routeName) it would push a new route to the stack.

对于v2:

Now navigate(routeName) will first try to find an existing instance of the route and jump to that if it exists, otherwise it will push the route to the stack.

导航>如果存在则转到页面实例或推送新实例

push>推送一个新实例,即使已存在一个实例

© www.soinside.com 2019 - 2024. All rights reserved.