react Native 如何实现路由跳转,底部tab切换路由跳转,并隐藏页面路由系统头部显示

1. 安装相关所需要的库              

npm install @react-navigation/native
npm install @react-navigation/bottom-tabs
npm install @react-navigation/stack

   package.json 文件生成如下         

2.在app入口文件设置引用

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

3.初始化

// 引入需要的组件
import {
  Text,
  View,
  Image,
} from 'react-native';

//初始化基础路由
const Stack = createStackNavigator(); //基础路由
const Tab = createBottomTabNavigator();  //tab页面路由


4.导入需要展示跳转的组件

import home from  './pages/home /index'; // 确保路径正确  (tab首页
import tab2 './pages/tab2 /index'; // 确保路径正确 (tab第2页面
import HelloWorld from './pages/hello/hello'; // 确保路径正确
import Hi from './pages/hello/hi'; // 确保路径正确
import MyComponent from './pages/apii'; // 确保路径正确

5. 设置项目基础路由设置

function App(): React.JSX.Element {
  return (
      <NavigationContainer>
        <Stack.Navigator mode="modal">
            <Stack.Screen name="home" options={{title:'消息',headerShown: false}} component={TabBar} />
            <Stack.Screen name="hello " options={{title:'用户列表'}} component={HelloWorld }/>
            <Stack.Screen name="hi" options={{title:'所有订单'}} component={Hi}/>
        </Stack.Navigator>
      </NavigationContainer>
  );
}

//headerShown 隐藏默认头部name文字显示
//NavigationContainer 需要在根部,否则页面会有问题
//Stack.Screen 配置的是我需要路由跳转的页面

6.在第一个路由配置里设置编写TabBar页面

function TabBar(){
  return(
    <Tab.Navigator
      screenOptions={({route})=>({
        tabBarIcon:({focused,size,color})=>{
          let icon;
            //自定义tabbar 样式图片
          if(route.name==="消息"){
            icon = focused ?
             (
              <Image
                source={ require('./asstes/images/home1.png') }
                style={{ width: 25, height: 25, }} />
            ) : ( <Image
              source={ require('./asstes/images/home.png') }
              style={{ width: 25, height: 25, }} />)
          }
          return icon;
        }
      })}
    >
      <Tab.Screen name="消息" component={Message} options={{
        tabBarLabel: '消息',
        headerShown: false, // 这里设置为false隐藏标题
      }}/>
      <Tab.Screen name="我的" component={MyComponent} options={{
        tabBarLabel: '我的',
        // tabBarIcon: ({ color, size }) => (
        //   <Ionicons name="home" color={color} size={size} />
        // ),
        headerShown: false, // 这里设置为false隐藏标题
      }}/>
    </Tab.Navigator>
  )
}

tabbar页面

现在tabbar页面设置以及点击跳转就已经可以实现了

7.实现基础路由页面跳转

在需要跳转的组件内添加方法

例如:

const Home= ({navigation}) => {
    const renderItem = ({ item }) => <Item navigation={navigation} title={item.title}  sTitle={item.sTitle} dtime={item.dtime}/>;

    return (
      <View style={[styles.container,tw`bg-white`]}>
        <Button
          title="Go to Details"
          onPress={() => navigation.navigate('hello')}
            />
        </View>
        );
    };

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值