结合createStackNavigator和createBottomTabNavigator吗?

时间:2018-08-23 22:52:39

标签: javascript reactjs react-native react-native-navigation

场景:

  • 我有一个使用三个选项卡进行导航的应用程序(学校,管理员,家庭);
  • 我现在正尝试添加其他屏幕,这些屏幕将没有相应的标签。这些屏幕将导航为使用this.props.navigation.navigate('ChatScreen')

问题 -根据过去的解决方案,每当我添加一个屏幕时,都会在该屏幕上添加一个标签。

Todo:

  • 我想将标签页以及其他常规(非标签页)屏幕都放在导航堆栈中。

    • 我希望标签和标头都保持不变
  • 我无法将两者结合在一起,并且尝试了很多 下面的代码的变体。

尝试代码:

const School = createStackNavigator({
  School: { 
  screen: SchoolScreen, 
  navigationOptions: {
  headerTitle: <CustomHeaderTitle screen='school'/>
    }
  }
 });

const Admin = createStackNavigator(
  { Admin: {
  screen: AdminScreen,
  navigationOptions: {
    headerTitle: <CustomHeaderTitle screen='admin' />
      }
    }
  });

 const Family = createStackNavigator(
  {
   Family: {
      screen: FamilyScreen,
      navigationOptions: {
      headerLeft: null,
      headerTitle: <CustomHeaderTitle screen='family' />
      }
     }
    }
);

const ChatStack = createStackNavigator({
  CreateChat: CreateChatScreen
});

const TabStack = createBottomTabNavigator(
  {
    School: School,
    Admin: Admin,
    Family: Family
  },
  {
navigationOptions: ({ navigation }) => ({

  tabBarIcon: () => {
    const { routeName } = navigation.state;
    return <Image id={1} source={require('./app/img/school_logo.png')} />
  },
  tabBarLabel: navigation.state.routeName
}),
tabBarOptions: {
  activeTintColor: 'tomato',
  inactiveTintColor: 'gray',
  style: {
    backgroundColor: 'black',
    height: 55
  }
}

  }
);

const RootStack = createStackNavigator({
  Root: ChatStack,
  Tabs: TabStack
})

export default class App extends Component {
  render() { return (
      <Provider store={store}>
        <RootStack />
      </Provider>
    );
  }
}


很抱歉,与之抗争一段时间后,我无法格式化此代码。


感谢您的任何帮助或建议!
请提出建议。

3 个答案:

答案 0 :(得分:1)

贷给未命名的redditor:

您必须将整个堆栈嵌套在标签导航器的每个屏幕中。据我所知,如果嵌套在不同的TabNavigator屏幕中,则无法访问StackNavigator中的不同屏幕。

例如,如果您希望能够从SchoolScreen导航到聊天屏幕,则必须将该组件包括在School导航器中。

 const School = createStackNavigation({
   School: {
       screen: SchoolScreen
   },
   SchoolChat: {
       screen: CreateChatScreen
   } 
 });

从那里,您的主TabNavigator应该看起来差不多

const TabStack = createBottomTabNavigator({
    School: School
});

答案 1 :(得分:0)

当TabStack处于焦点状态时,您应该隐藏RootStack标头

TabStack.navigationOptions = {
  // Hide the header from root stack
  header: null,
};

,您不需要将堆栈添加到CreateChatScreen

const RootStack = createStackNavigator({
  Tabs: TabStack,
  ChatScreen: CreateChatScreen,
})

答案 2 :(得分:0)

在响应本机导航5

import React from 'react';
import {Text} from 'react-native';
import {createStackNavigator} from '@react-navigation/stack';
import {NavigationContainer} from '@react-navigation/native';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';


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


function Scr(){
  return <Text>hello</Text>;
}
function MyTabs() {
  return (
    <Tab.Navigator
      initialRouteName="Expolre"
      tabBarOptions={{
        activeTintColor: '#414757',
      }}>
      <Tab.Screen name="Expolre" component={Scr} />
    </Tab.Navigator>
  );
}

export default function Routing() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen
          name="login"
          component={Scr}
          options={{header: () => null}}
        />
        <Stack.Screen
          name="dashboard"
          component={MyTabs}
          options={{header: () => null}}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
}