react-native 配置底部导航栏和路由

1.新增路由文件和底部导航栏文件, /src/route/index.tsx 、 /src/components/BottomTabs/BottomTabs.tsx


image.png
  1. /src/components/BottomTabs/BottomTabs.tsx
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import {NavigationContainer} from '@react-navigation/native';
import Index from '../../views/Index/Index';
import Mine from '../../views/Mine/Mine';
import Classify from '../../views/Classify/Classify';

import {Icon} from '@rneui/themed';

export type BottomTabParamList = {
  Index: undefined;
  Mine: undefined;
  Classify: undefined;
};

const Tab = createBottomTabNavigator<BottomTabParamList>();

function BottomTabs() {
  return (
    <Tab.Navigator
  //    screenOptions={({route}) => ({
  //      tabBarIcon: ({focused, color, size}) => {
   //       let iconName = '';

    //      if (route.name === 'Index') {
      //      iconName = focused ? 'add-circle' : 'add-circle-outline';
      //    } else if (route.name === 'Classify') {
      //      iconName = focused ? 'person' : 'person-outline';
      //    }
      //    return <Icon name={iconName} size={size} color={color} />;
      //  },
      //})}
      >
      <Tab.Screen
        name="Index"
        component={Index}
        options={{
          headerShown: false,
          tabBarLabel: '首页',
          tabBarIcon: ({color, size}) => (
            <Icon name="delete" color={'#e00000'} size={30} />
          ),
          tabBarBadge: 3,
        }}
      />
      <Tab.Screen
        name="Classify"
        component={Classify}
        options={{
          tabBarLabel: '分类',
          tabBarIcon: ({color, size}) => (
            <Icon name="delete" color={'#e00000'} size={30} />
          ),
          tabBarBadge: 3,
        }}
      />
      <Tab.Screen
        name="Mine"
        component={Mine}
        options={{
          tabBarLabel: '我的',
          tabBarIcon: ({color, size}) => (
            <Icon name="delete" color={'#e00000'} size={30} />
          ),
          tabBarBadge: 3,
        }}
      />
    </Tab.Navigator>
  );
}

export default BottomTabs;

  1. /src/route/index.tsx
import {
  createStackNavigator,
  StackNavigationProp,
} from '@react-navigation/stack';
import {NavigationContainer} from '@react-navigation/native';

import Index from '../views/Index/Index';
import Mine from '../views/Mine/Mine';
import Classify from '../views/Classify/Classify';
import Detail from '../views/Detail/Detail';
import BottomTabs from '../components/BottomTabs/BottomTabs';

type RootStackParamList = {
  BottomTabs: undefined; // 这里改一下
  Detail: undefined;
};

export type RootStackNavigation = StackNavigationProp<RootStackParamList>;

const Stack = createStackNavigator();

const AppNavigator = () => {
  return (
    <NavigationContainer>
      <Stack.Navigator
        initialRouteName="Index"
        screenOptions={{
          headerTitleAlign: 'center',
        }}>
        <Stack.Screen
          name="BottomTabs"
          component={BottomTabs}
          options={{
            headerShown: false,
          }}
        />
        <Stack.Screen name="Detail" component={Detail} />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

export default AppNavigator;

  1. 在app.tsx里引入route。


    image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容