简体   繁体   English

React Native 底栏选项卡更改路由变量

[英]React Native bottom bar tab change route variable

I want to build a bottom tab bar that is same in every screen but I want bottom Tabs' route change.我想构建一个在每个屏幕中都相同的底部标签栏,但我希望底部标签的路线改变。 ex.前任。 In Screen1 in bottom tab bar Tab route in 'Screen2' and when navigate in Screen2 then tab route change from Screen2 to Screen3.在底部选项卡栏中的 Screen1 中“Screen2”中的选项卡路由,当在 Screen2 中导航时,选项卡路由从 Screen2 更改为 Screen3。 But always appear the same bottom tab bar.但总是出现相同的底部标签栏。

One idea is to create bottom tab bar with Tab route {ScreenRoute} and in every Screen change ScreenRoute variable.一个想法是使用 Tab route {ScreenRoute} 创建底部标签栏,并在每个 Screen 更改 ScreenRoute 变量中。 ex.前任。 In Screen1 var ScreenRoute = 'Screen2', In Screen2 var ScreenRoute = 'Screen3' etc.在 Screen1 中 var ScreenRoute = 'Screen2',在 Screen2 中 var ScreenRoute = 'Screen3' 等。

You could use a tabPress listener and prevent the default navigation event using the preventDefault function.您可以使用tabPress侦听器并使用preventDefault function 来阻止默认导航事件。 Furthermore, use a state to indicate the current route.此外,使用 state 指示当前路线。 Use a reference to the NavigationContainer in order to navigate to a desired screen.使用对NavigationContainer的引用来导航到所需的屏幕。

Here is a minimal example.这是一个最小的例子。

const Tab = createBottomTabNavigator();
const Stack = createStackNavigator();

export default function App() {
  const navigationRef = React.useRef();
  const [routeName, setRouteName] = React.useState('');
  return (
    <NavigationContainer
      ref={navigationRef}
      onReady={() => {
        const currentRoute = navigationRef.current.getCurrentRoute();
        setRouteName(currentRoute.name);
      }}>
      <Tab.Navigator>
        <Tab.Screen
          name="A"
          component={StackA}
          listeners={{
            tabPress: (e) => {             
              e.preventDefault()
              if (routeName === "ScreenA") {
                setRouteName("ScreenA2");
                navigationRef.current.navigate("ScreenA2")
              } else if (routeName === "ScreenA2") {
                setRouteName("ScreenA3");
                navigationRef.current.navigate("ScreenA3")
              } else {
                setRouteName("ScreenA")
                navigationRef.current.navigate("ScreenA")
              }
            },
          }}
        />
      </Tab.Navigator>
    </NavigationContainer>
  );
}

The navigators and screens are defined as follows.导航器和屏幕定义如下。

function ScreenA() {
  return (
    <View>
      <Text>ScreenA</Text>
    </View>
  );
}

function ScreenA2() {
  return (
    <View>
      <Text>ScreenA2</Text>
    </View>
  );
}

function ScreenA3() {
  return (
    <View>
      <Text>ScreenA3</Text>
    </View>
  );
}

function StackA() {
  return (
    <Stack.Navigator>
      <Stack.Screen name="ScreenA" component={ScreenA} />
      <Stack.Screen name="ScreenA2" component={ScreenA2} />
      <Stack.Screen name="ScreenA3" component={ScreenA3} />
    </Stack.Navigator>
  );
}

Here is a little snack . 这里有一点点心

The first press on the tab will navigate to ScreenA2 , the second press will navigate to ScreenA3 , the third press will start on ScreenA again.选项卡上的第一次按下将导航到ScreenA2 ,第二次按下将导航到ScreenA3 ,第三次按下将再次在ScreenA上开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在React Native中将底部栏标签视为按钮? - How to treat a bottom bar tab as a button in React Native? 无法在特定屏幕上的 React Native 中隐藏或显示选项卡底栏 - Not able to hide or show tab bottom bar in React Native on particular screen 如何在 React Native 中制作像底部标签栏这样的复杂设计? - How to make complicated designs like bottom tab bar in react native? 当标签在 React Native 中处于活动状态或未处于活动状态时更改标签栏图标 - Change tab bar icons when a tab is active or not in React Native 如何在 React Native 中更改 Botton 标签栏背景颜色? - How to change Botton tab bar Background Color in React Native? 如何使用本机反应更改标签栏中图标的色调颜色 - how to change tint colour of an icon in tab bar with react native 反应本机导航选项卡栏 - react native navigation tab bar 打开键盘时反应原生底部标签栏向上推 - React native bottom tab bar pushing itself up when opening keyboard 在 React native 中创建自定义底部选项卡导航器 - Create custom bottom tab navigator in React native React Native TabBarIcon 未显示在底部选项卡导航中 - React Native TabBarIcon is not showing in the bottom tab naviation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM