React Navigation V5 隐藏底部标签

Ash*_*t01 11 javascript react-native react-native-navigation react-navigation

我希望能够使用 React Native Navigation v5 隐藏屏幕上的选项卡。

我一直在阅读文档,但似乎他们并没有为 v5 更新这个,它指的是 < v4 的做事方式。

这是我的代码:

import Home from './components/Home';
import SettingsScreen from './components/Settings';
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { createStackNavigator } from '@react-navigation/stack';

const SettingsStack = createStackNavigator();
const ProfileStack  = createStackNavigator();

function SettingsStackScreen() {
    return (
        <SettingsStack.Navigator>
            <SettingsStack.Screen name="Settings" component={SettingsScreen} />
        </SettingsStack.Navigator>
    )
}

function ProfileStackScreen() {
    return (
        <ProfileStack.Navigator>
            <ProfileStack.Screen name="Home" component={Home} />
        </ProfileStack.Navigator>
    )
}

const Tab = createBottomTabNavigator();

export default function App() {
  return (
    <NavigationContainer>
      <Tab.Navigator>
        <Tab.Screen name="Home" component={ProfileStackScreen} />
        <Tab.Screen name="Settings" component={SettingsStackScreen} />
      </Tab.Navigator>
    </NavigationContainer>
  );
}
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情:

  1. 访问函数的选项并以这种方式隐藏。
  2. 将 tabBarVisible 作为道具传递给屏幕。

我要问的是,在 React Navigation v5 中隐藏屏幕上的选项卡的正确方法是什么?

小智 6

tabbarvisible-option-在 React navigation v5 及以上版本中不再存在tabBarStyle: { display: 'none' }您可以通过在屏幕选项中指定要隐藏底部选项卡来实现相同的行为

export default function App() {
  return (
    <NavigationContainer>
      <Tab.Navigator>
        <Tab.Screen name="Home" component={ProfileStackScreen} />
        <Tab.Screen options={{tabBarStyle:{display:'none'}}} name="Settings" component={SettingsStackScreen} />
      </Tab.Navigator>
    </NavigationContainer>
  );
}
Run Code Online (Sandbox Code Playgroud)


MBa*_*ach 4

假设您想在进入“设置”时隐藏选项卡。只需在构造函数中添加导航即可:

function SettingsStackScreen({ navigation }) {
    navigation.setOptions({ tabBarVisible: false })
    return (
        <SettingsStack.Navigator>
            <SettingsStack.Screen name="Settings" component={SettingsScreen} />
        </SettingsStack.Navigator>
    )
}
Run Code Online (Sandbox Code Playgroud)

这段代码应该可以工作。

  • 类型错误:navigation.setOptions 不是函数。“navigation.setOptions”未定义 (4认同)