问题描述
我是 React Native 的新手,目前正在研究 React Native Navigation Docs.我想知道:navigation.push()
和 navigation.navigate()
有什么区别?
我试图自己找出来,但他们似乎完成了完全相同的事情......
I tried finding it out by myself, but they seem to accomplish the exact same thing...
推荐答案
If you check the documentation for push
, there is an explanation of how different they are.
Push 动作在栈顶添加一个路由并向前导航到它.这与导航不同,导航将返回到如果组件已经安装在堆栈中的较早位置.推将始终添加在顶部,因此可以多次安装组件.
我们可以以 Instagram 为例;
We can take Instagram for example;
考虑导航到用户的个人资料.然后您可以检查用户的关注者,然后您也可以导航到他们的个人资料.如果您仅使用 navigate
操作执行相同的操作,则当您从关注者列表屏幕单击用户的个人资料时,将导航到上一个个人资料,但如果您使用 push
将新屏幕推送到堆栈并显示正确的配置文件.这样你就可以goBack
到第一个屏幕.
Consider navigating to a user's profile. Then you can check user's followers and then you can navigate to their profile's too.If you do this same actions with only navigate
action, when you click on an user's profile from the followers list screen will navigate to the previous profile but if you use push
it will push a new screen to the stack and show the correct profile. This way you can goBack
to the first screen.
这篇关于React Navigation V2:navigation.push 和 navigation.navigate 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!