react-navigation 6.x 学习(3)

  • Tab navigation
    • 基于tab导航的实例
    • 自定义外观
    • tabs之间跳转
    • 每个tab的堆栈导航器
  • Drawer navigation
    • 打开和关闭drawer

Tab navigation

移动应用中最常见的导航是基于Tab的导航。这可以是屏幕底部的标签,也可以标题下面的标签(甚至可以代替标题)。

本指南涵盖了createBottomTabNavigator。您也可以使用createMaterialBottomTabNavigator和createMaterialTopTabNavigator来为您的应用程序添加标签。

在继续之前,首先安装@react-navigation/bottom-tabs:

npm install @react-navigation/bottom-tabs
yarn add @react-navigation/bottom-tabs

基于tab导航的实例

import * as React from 'react';
import { Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';function HomeScreen() {return (<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}><Text>Home!</Text></View>);
}function SettingsScreen() {return (<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}><Text>Settings!</Text></View>);
}const Tab = createBottomTabNavigator();export default function App() {return (<NavigationContainer><Tab.Navigator><Tab.Screen name="Home" component={HomeScreen} /><Tab.Screen name="Settings" component={SettingsScreen} /></Tab.Navigator></NavigationContainer>);
}

自定义外观

这类似于您定制堆栈导航器的方式——有些属性是在初始化选项卡导航器时设置的,而其他属性则可以在每个屏幕的选项(options)中定制。

// You can import Ionicons from @expo/vector-icons/Ionicons if you use Expo or
// react-native-vector-icons/Ionicons otherwise.
import Ionicons from 'react-native-vector-icons/Ionicons';// (...)export default function App() {return (<NavigationContainer><Tab.NavigatorscreenOptions={({ route }) => ({tabBarIcon: ({ focused, color, size }) => {let iconName;if (route.name === 'Home') {iconName = focused? 'ios-information-circle': 'ios-information-circle-outline';} else if (route.name === 'Settings') {iconName = focused ? 'ios-list-box' : 'ios-list';}// You can return any component that you like here!return <Ionicons name={iconName} size={size} color={color} />;},tabBarActiveTintColor: 'tomato',tabBarInactiveTintColor: 'gray',})}><Tab.Screen name="Home" component={HomeScreen} /><Tab.Screen name="Settings" component={SettingsScreen} /></Tab.Navigator></NavigationContainer>);
}

1.tabBarIcon是底部标签导航器中支持的选项。所以我们可以在屏幕(Screen)组件的options属性中使用它,但在这里我们选择把它放在Tab的screenOptions属性中。导航器以便集中配置图标。

2.tabBarIcon是一个提供 focused, color, size参数的函数。
3.tabBarActiveTintColor和tabBarInactiveTintColor,可以在这里更改它们颜色。传递给tabBarIcon的颜色是活动的还是非活动的,这取决于聚焦状态(聚焦是活动的)。size是标签栏所期望的图标大小。

给icons添加徽章

有时我们想在一些图标上添加徽章。你可以使用tabBarBadge选项:

<Tab.Screen name="Home" component={HomeScreen} options={{ tabBarBadge: 3 }} />

tabs之间跳转

从一个tab跳转到另一个tab,也是使用 navigation.navigate

function HomeScreen({ navigation }) {return (<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}><Text>Home!</Text><Buttontitle="Go to Settings"onPress={() => navigation.navigate('Settings')}/></View>);
}function SettingsScreen({ navigation }) {return (<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}><Text>Settings!</Text><Button title="Go to Home" onPress={() => navigation.navigate('Home')} /></View>);
}

每个tab的堆栈导航器

每个标签中都有独立的导航栈

import * as React from 'react';
import { Button, Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';function DetailsScreen() {return (<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}><Text>Details!</Text></View>);
}function HomeScreen({ navigation }) {return (<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}><Text>Home screen</Text><Buttontitle="Go to Details"onPress={() => navigation.navigate('Details')}/></View>);
}function SettingsScreen({ navigation }) {return (<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}><Text>Settings screen</Text><Buttontitle="Go to Details"onPress={() => navigation.navigate('Details')}/></View>);
}const HomeStack = createNativeStackNavigator();function HomeStackScreen() {return (<HomeStack.Navigator><HomeStack.Screen name="Home" component={HomeScreen} /><HomeStack.Screen name="Details" component={DetailsScreen} /></HomeStack.Navigator>);
}const SettingsStack = createNativeStackNavigator();function SettingsStackScreen() {return (<SettingsStack.Navigator><SettingsStack.Screen name="Settings" component={SettingsScreen} /><SettingsStack.Screen name="Details" component={DetailsScreen} /></SettingsStack.Navigator>);
}const Tab = createBottomTabNavigator();export default function App() {return (<NavigationContainer><Tab.Navigator><Tab.Screen name="Home" component={HomeStackScreen} /><Tab.Screen name="Settings" component={SettingsStackScreen} /></Tab.Navigator></NavigationContainer>);
}

Drawer navigation

导航中的常见模式是从左边(有时是右边)使用抽屉在屏幕之间导航。

安装

npm install @react-navigation/drawer
yarn add @react-navigation/drawer

也需要安装react-native-gesture-handler and react-native-reanimated.

npm install react-native-gesture-handler react-native-reanimated
或
yarn add react-native-gesture-handler react-native-reanimated

在index.js或App.js中引入文件

import 'react-native-gesture-handler';

基于drawer的实例

import * as React from 'react';
import { Button, View } from 'react-native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';function HomeScreen({ navigation }) {return (<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}><ButtononPress={() => navigation.navigate('Notifications')}title="Go to notifications"/></View>);
}function NotificationsScreen({ navigation }) {return (<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}><Button onPress={() => navigation.goBack()} title="Go back home" /></View>);
}const Drawer = createDrawerNavigator();export default function App() {return (<NavigationContainer><Drawer.Navigator initialRouteName="Home"><Drawer.Screen name="Home" component={HomeScreen} /><Drawer.Screen name="Notifications" component={NotificationsScreen} /></Drawer.Navigator></NavigationContainer>);
}

打开和关闭drawer

navigation.openDrawer();
navigation.closeDrawer();

切换drawer

navigation.toggleDrawer();

react-navigation 6.x 学习(3)相关推荐

  1. 使用Redux在React Navigation App中管理状态

    by Andrea Bizzotto 通过安德里亚·比佐托(Andrea Bizzotto) 使用Redux在React Navigation App中管理状态 (Managing State in ...

  2. RN路由-React Navigation组件5.x-基本原理(中文文档)

    ##引言 React Native路由导航,有它就够了!该文档根据React Navigation文档翻译,有些内容会根据自己的理解进行说明,不会照搬直译,若发现理解有问题的地方,欢迎大家提点!由于本 ...

  3. React Native小白入门学习路径——五

    React Native小白入门学习路径--五 序 "哦天呐!" 这句话成了我在实验室的口头禅, 老师可能觉得我们都是大神吧,都还在看着基础就给布置了那么多任务:写一个RN的TDD ...

  4. react navigation 中使用goBack()跳转到指定页面

    一.适用场景: 在app端开发的时候,相反回到某一个页面的时候保持跳转页面的所有状态不更新,也就是说不触发新的生命周期. 例如:A-->B-->C-->D 要想从D页面直接返回到B页 ...

  5. 【react】react18的学习(三)--hooks组件

    上一篇:[react]react18的学习(二)-三种组件 1.useState:使函数组件可以使用并修改 state import { useState } from 'react' const [ ...

  6. React Navigation 基本使用

    React Navigation 基本使用 参考资料 https://reactnavigation.org/docs/ 环境搭建 Minimum requirementsreact-native & ...

  7. React Native v0.55 学习笔记1

    React Native v0.55 学习笔记1 学习内容来自官网文档0.55版本 RN 是基于 React 的思想,相比于 web 的一些组件,RN 使用的是基于原生( android.ios )的 ...

  8. React Navigation 路由导航库升级 5.x

    当前版本:3.0.0 升级版本:5.12.8 安装依赖 $ yarn add   @react-navigation/native   @react-navigation/stack   @react ...

  9. react navigation中使用goBack返回指定页面

    goBack 首先, 现在看一下react navigation官网中对goBack的描述: goBack的参数为页面的key, 这个key是系统随机分配的, 而不是手动设置的routeName, 所 ...

  10. React Native跨平台开发学习笔记

    App的分类(按开发方式) 大致可以分为这3种: native app(原生app:ios或安卓)原生应用程序 原生应用程序外观和运行起来(性能)是最佳的.可以访问本地资源,开法成本高.发布审核周期长 ...

最新文章

  1. 中国电信的新媒体营销尝试
  2. html中加载解析,HTML页面加载和解析流程详细介绍
  3. C语言【将一个文本文件中的全部信息显示到屏幕上】
  4. arrayQueue
  5. initial、inherit、unset、revert和all
  6. hadoop没有datanode_Hadoop运行在Kubernetes平台实践
  7. R语言基础入门(6)之向量下标和子集
  8. Vue项目中自动将px转换为rem
  9. jsonpath 判断是否包含_mysql json 判断某个key是否存在
  10. 如何在macOS Monterey、iOS 15 中使用Apple Notes标签?
  11. 数据库系统 图书管理系统 C语言
  12. 思科CCNP在OSPF中设置LSA的简单命令汇总
  13. http://www.2cto.com/ 红黑联盟
  14. python风格logo_Python十分钟制作属于你自己的个性logo
  15. 保护模式下的CPL,RPL,DPL与特权级检查(二)
  16. 自问自答系列——商城相关
  17. 微信小程序开发与oss防盗链
  18. 手机号登录和微信登录
  19. 美国移民局宣布H-1b签证新规 高学历申请者受益
  20. memory interleaving(内存交织)

热门文章

  1. 伤害世界不显示服务器,伤害世界Hurt world服务器挑选指南 不卡不清档无权限服务器挑选教程...
  2. 【区块链实战】区块链在哪些行业得到了应用
  3. mysql随机生成名字,起名不求人
  4. 桌面以及文件资源管理器无限重启的解决
  5. 桌面窗口管理器(dwm.exe)占用高内存的解决方法
  6. 网站有上传后门木马的漏洞怎么解决
  7. C#getPixel和内存法读取灰度图信息
  8. 分类指标计算 Precision、Recall、F-score、TPR、FPR、TNR、FNR、AUC、Accuracy
  9. 线性回归(Linear regression)算法
  10. mac 我用Mac的这一年