[英]How do I add a navigation button to a React Navigation Stack header with nested Bottom Tab Navigator?
I am trying to build a mobile app in react-native and I'm having some problems setting up React Navigation.我正在尝试在 react-native 中构建一个移动应用程序,但在设置 React Navigation 时遇到了一些问题。
What I want to achieve is a Bottom Tab Navigator that Navigates to the 'Home' screen and the 'Profile' Screen.我想要实现的是导航到“主”屏幕和“配置文件”屏幕的底部选项卡导航器。 From the 'Home' screen, there should be a button to navigate to the 'Settings' screen in the Header.
在“主页”屏幕上,应该有一个按钮可以导航到 Header 中的“设置”屏幕。
I have got to the point where I have a Bottom Tab Navigator that can successfully navigate between the 'Home' and 'Profile' screens, as well as a button on the header for the Settings screen using the Stack navigation header.我已经到了可以在“主页”和“个人资料”屏幕之间成功导航的底部选项卡导航器的地步,以及使用堆栈导航 header 的设置屏幕的 header 上的按钮。 However, I am having trouble navigating to the 'Settings' screen with this button.
但是,我无法使用此按钮导航到“设置”屏幕。
My code for the Stack navigator is:我的堆栈导航器代码是:
const MainStackNavigator = () => {
return (
<Stack.Navigator screenOptions={screenOptionStyle}>
<Stack.Screen
name="Home"
component={HomeScreen}
options = { ({navigation}) => ({
title: "Home",
headerStyle: {
backgroundColor: '#ff6600',
},
headerRight: () => (
<Button
onPress={() => navigation.navigate(SettingScreen)}
title="Settings"
color="#fff"
/>
)
})}
/>
<Stack.Screen name="Settings" component={SettingScreen} />
</Stack.Navigator>
);
} }
When I click on the Settings button, I get the error:当我点击设置按钮时,我收到错误:
"The action 'NAVIGATE' with payload undefined was not handled by any navigator. “未定义有效负载的操作‘NAVIGATE’未被任何导航器处理。
Do you have a screen named 'SettingScreen'?"你有一个名为“SettingScreen”的屏幕吗?”
Upon looking for a solution to this error I found this article: Nesting Navigators在寻找此错误的解决方案时,我发现了这篇文章: Nesting Navigator
It recommends keeping nested navigators to a minimal.它建议将嵌套导航器保持在最低限度。 Is my method even the right way about going for this UI design?
我的方法甚至是进行此 UI 设计的正确方法吗? Is there a way to achieve this with only using one navigator?
有没有办法只使用一个导航器来实现这一点?
After some time trying to solve this I found the problem was quite silly of me.经过一段时间尝试解决这个问题后,我发现这个问题对我来说很愚蠢。 navigation.navigate takes the name of the screen to navigate to, but I was giving it the component.
navigation.navigate 采用要导航到的屏幕名称,但我给它的是组件。
To fix the problem I changed为了解决我改变的问题
onPress={() => navigation.navigate(SettingScreen)}
to至
onPress={() => navigation.navigate('Settings')}
Add this below your render method!在你的渲染方法下面添加这个!
render () {
const { navigate } = this.props.navigation;
}
And then in the onPress onPress={() => navigate(SettingScreen)}
然后在onPress
onPress={() => navigate(SettingScreen)}
Hopefully this helps希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.