问题描述
使用 React 导航,从导航器中的屏幕导航到不同导航器中的屏幕.
Using react navigation, navigate from a screen in a navigator to a screen in a different navigator.
如果我有以下导航器结构:
If I have the following Navigator structure:
- 父导航器
- 嵌套导航器 1
- 屏幕 A
- 屏幕 B
- 屏幕 C
- 屏幕 D
如何从嵌套导航器 2 下的屏幕 D 转到嵌套导航器 1 下的屏幕 A?现在,如果我尝试
navigation.navigate
从屏幕 D 到屏幕 A,将会出现一个错误,表明它不知道屏幕 A,只知道屏幕 C 和 D.how do I go from screen D under nested navigator 2, to screen A under nested navigator 1? Right now if I try to
navigation.navigate
to screen A from screen D there will be an error that says it doesn't know about a screen A, only screen C and D.我知道在本网站以及 GitHub 的各个地方都以各种形式询问了这个问题(https://github.com/react-navigation/react-navigation/issues/983、https://github.com/react-navigation/react-navigation/issues/335#issuecomment-280686611) 但对于如此基本的东西,有一个缺乏明确的答案并滚动浏览数百条 GitHub 评论以寻找解决方案并不是很好.
I know this has been asked in various forms in various places on this site as well as GitHub(https://github.com/react-navigation/react-navigation/issues/983, https://github.com/react-navigation/react-navigation/issues/335#issuecomment-280686611) but for something so basic, there is a lack of clear answers and scrolling through hundreds of GitHub comments searching for a solution isn't great.
也许这个问题可以为遇到这个非常常见问题的每个人编码如何执行此操作.
推荐答案
更新:对于 React Navigation v5,请参阅 @mahi-man 的回答.
Update: For React Navigation v5, see @mahi-man's answer.
您可以使用 第三个参数 导航以指定子操作.
You can use the third parameter of
navigate
to specify sub actions.例如,如果您想从嵌套导航器 2 下的屏幕 D 转到嵌套导航器 1 下的屏幕 A:
For example, if you want to go from screen D under nested navigator 2, to screen A under nested navigator 1:
this.props.navigation.navigate( 'NestedNavigator1', {}, NavigationActions.navigate({ routeName: 'screenB' }) )
还要检查:https://reactnavigation.org/docs/nesting-navigators/
这篇关于如何在反应导航中的不同嵌套堆栈之间导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- 嵌套导航器 1