简体   繁体   English

如何使用嵌套的底部选项卡导航器将导航按钮添加到 React 导航堆栈 header?

[英]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.

相关问题 反应导航 | 如何从嵌套在 Stack Navigator 中的子屏幕更改 Tab Navigator 上的按钮? - React Navigation | How do I change the buttons on a Tab Navigator from a child screen nested in a Stack Navigator? 如何将底部选项卡导航器添加到堆栈导航中的一个屏幕? - How to add bottom tab navigator to one screen inside stack navigation? 如何在 React Native 的嵌套堆栈导航中的 header 上添加按钮 - How to add button on header in nested stack navigation in react native React-当我有底部标签导航时如何添加标题 - React - how to add header when I have bottom tab navigation 嵌套选项卡导航器中的 React Navigation header 标题 - React Navigation header title in nested tab navigator 我们如何在反应导航中点击底部选项卡导航器上的相应选项卡按钮时实现滚动到顶部功能? - How do we implement Scroll to top functionality on tapping of the corresponding tab button on a bottom tab navigator in react navigation? 如何在反应原生的嵌套堆栈导航器中隐藏材料底部选项卡导航器 - How do I hide material bottom tab navigator in a nested stack navigator in react native 如何使用React本机导航创建嵌套导航? (示例:选项卡导航器中的堆栈导航器) - How to use react native navigation to create a nested navigation? (Example: Stack navigator inside a Tab navigator) 当同时使用:Stack Navigator 和 BottomTab Navigator 时,如何在 Stack Navigation Screen 中隐藏底部选项卡? - When using both: Stack Navigator and BottomTab Navigator how do I hide the bottom tab when inside a Stack Navigation Screen? 如何使用 React 导航 5.X 将堆栈导航与底部选项卡导航器一起使用 - How to use stack navigation with bottom tab navigator using React navigation 5.X
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM