react native 实现自定义底部导航与路由文件配置

首先先把需要的一些库引入

yarn install @react-navigation/native

yarn install react-native-screens react-native-safe-area-context

yarn install @react-navigation/native-stack

yarn add @react-navigation/bottom-tabs

创建路由文件及四个底部导航页面

router文件下的bottomTab.jsx

这个文件主要就是app的底部导航配置

复制代码
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import { Image} from 'react-native';
//  引入自定义图片
import Home from '../views/home/index';
import User from '../views/user/index';
import Parking from '../views/parking/index';
import Detail from '../views/detail/index';

const Tab = createBottomTabNavigator();
export default function TabBar() {
  return (
    <Tab.Navigator
      screenOptions={({route}) => ({
        tabBarIcon: ({focused}) => {
          let iconName;
           // 自定义图标
          if (route.name === '首页') {
            iconName = focused ? require('../assets/icon/bottom/sy_seleted.png') : require('../assets/icon/bottom/sy.png');
          }else if(route.name === '车位'){
            iconName = focused ? require('../assets/icon/bottom/zcw_seleted.png') : require('../assets/icon/bottom/zcw.png');
          }else if(route.name === '订单'){
            iconName = focused ? require('../assets/icon/bottom/dd_seleted.png') : require('../assets/icon/bottom/dd.png');
          }else{
            iconName = focused ? require('../assets/icon/bottom/wd_seleted.png') : require('../assets/icon/bottom/wd.png');
          }
          return <Image source={iconName}></Image>;
        },
      })}>
       // options={{headerShown: false}} 这个是  是否展示顶部导航 
      <Tab.Screen name="首1页" component={Home}  options={{headerShown: false}}/>
      <Tab.Screen name="车位" component={Parking} />
      <Tab.Screen name="订单" component={Detail} />
      <Tab.Screen name="我的" component={User} />
    </Tab.Navigator>
  );
}

router文件下的index.jsx

这里面就是存放项目中路由的地方

复制代码
import React from 'react';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import BottomTabBar from './bottomTab'; // 引入底部TAB栏
import Setting from '../views/setting/index'; 
const Stack = createNativeStackNavigator();
// stack路由配置
export default function Navigation() {
  return (
    <Stack.Navigator>
      // 把底部导航栏引入
      <Stack.Screen
        name={'Navigation'}
        component={BottomTabBar}
      />
      //  这里存放项目页面路由
      //  至于里面的配置 你们可以可以搜一下去按照自己项目需求去配置
      <Stack.Screen
        name={'Setting'}
        options={{
          title: '设置',
          headerStyle: {
            backgroundColor: 'black',
          },
          headerTintColor: '#fff',
          headerTitleStyle: {
            fontWeight: 'bold',
          },
        }}
        component={Setting}
      />
    </Stack.Navigator>
  );
}

最后在app.jsx中引入即可

复制代码
import React from 'react';
import {View, Text} from 'react-native';

import {NavigationContainer} from '@react-navigation/native';

import Navigation from './src/router';


const App = () => {
  return (
  
    <NavigationContainer>
      <Navigation />
    </NavigationContainer>
  );
};
export default App;

这个可以直接赋值粘贴只需要把文件创建一下即可

下面是文件目录

相关推荐
PYB31 天前
【Web·JS·基础】函数的使用和展运算符...objs
前端·javascript
默_笙1 天前
🚥 给 RAG 装一台心电监护仪:LangSmith 全链路观测(上)
前端·javascript
变与不变8061 天前
js同步和异步难点重点详解
开发语言·javascript·ecmascript
Dovis(誓平步青云)1 天前
多个链接不等于多份证据,新闻核验看板怎样合并来源
java·服务器·前端·javascript·人工智能·pdf·电脑
Dovis(誓平步青云)1 天前
几个方案来回选不定?做一个随时切换的候选推荐页
android·java·服务器·开发语言·javascript·数据库·智能化
可乐鸡翅yeah_1 天前
fmp4 碎片 MP4 格式 HLS 流,和传统 TS 分片有什么不一样
开发语言·javascript·ios·音视频·safari·m3u8·m3u8在线播放
ss2731 天前
AI全栈实战 | 2.3-01 组件库实战:Element Plus 怎么搭出专业界面,过度封装为什么成维护负担
前端·javascript·elementui·vue
福兮说1 天前
URL 编码的七个坑:c++ 传到后端变空格、%25 套娃、截断 emoji 直接报错
开发语言·前端·javascript·node.js·url
小的时候可菜了1 天前
JS 异步的本质:从单线程到 thenable(一条进化链)
开发语言·前端·javascript
可乐鸡翅yeah_1 天前
hls.js 配置 loadStartPosition,点播起始播放位置踩坑
开发语言·javascript·网络协议·ecmascript·m3u8在线