首页 文章

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

提问于
浏览
2

我'm new to React Native and I'目前正在研究React Native Navigation Docs . 我想知道: navigation.push()navigation.navigate() 有什么区别?

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

2 回答

  • 3

    如果您查看push的文档,可以解释它们的不同之处 .

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

    我们可以以Instagram为例;

    考虑导航到用户's profile. Then you can check user'的粉丝,然后您也可以导航到他们的 Profiles . 如果仅使用 navigate 操作执行相同的操作,当您从关注者列表屏幕单击用户的配置文件时,将导航到上一个配置文件,但如果您使用 push ,则会将新屏幕推送到堆栈并显示正确的配置文件 . 这样你就可以 goBack 到第一个屏幕 .

  • 1

    根据上一篇博客文章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>推送一个新实例,即使已存在一个实例

相关问题