導(dǎo)航庫(kù)React Navigation
? ? 功能:1.跳轉(zhuǎn)頁(yè)面
? ? ? ? ? 2.底部導(dǎo)航欄
? ? ? ? ? 3.頂部導(dǎo)航欄
? ? ? ? ? 4.身份認(rèn)證
一、createStackNavigator
? ? 1)配置多個(gè)頁(yè)面
? ? ? ? 1)在你的 React Native 項(xiàng)目中安裝react-navigation這個(gè)包
? ? ? ? ? ? yarn add react-navigation
? ? ? ? 2)導(dǎo)入依賴創(chuàng)建路由
? ? ? ? ? ? import { createStackNavigator } from 'react-navigation';
? ? ? ? 3)創(chuàng)建
? ? ? ? ? const RootStack = createStackNavigator(
? ? ? ? ? ? {
? ? ? ? ? ? ? Home: HomeScreen,
? ? ? ? ? ? ? Details: DetailsScreen,
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? initialRouteName: 'Home',
? ? ? ? ? ? }
? ? ? ? ? );
? ? ? ? ? 注:由于createStackNavigator函數(shù)會(huì)返回一個(gè)React組件
? ? 2)頁(yè)面切換
? ? ? ? <Button
? ? ? ? ? title="跳轉(zhuǎn)詳情頁(yè)面"
? ? ? ? ? onPress={() => this.props.navigation.navigate("Details")}
? ? ? ? />
? ? ? ? <Button
? ? ? ? ? title="跳轉(zhuǎn)自己本身Home頁(yè)面:navigate"
? ? ? ? ? onPress={() => this.props.navigation.navigate("Home")}
? ? ? ? />
? ? ? ? <Button
? ? ? ? ? title="跳轉(zhuǎn)自己本身Home頁(yè)面:push"
? ? ? ? ? onPress={() => this.props.navigation.push("Home")}
? ? ? ? />
? ? ? ? <Button
? ? ? ? ? title="返回按鈕"
? ? ? ? ? onPress={() => this.props.navigation.goBack()}
? ? ? ? />
? ? 3)頁(yè)面?zhèn)髦?
? ? ? ? 傳值數(shù)據(jù)A:
? ? ? ? ? ? <Button
? ? ? ? ? ? ? title="跳轉(zhuǎn)詳情頁(yè)面,實(shí)現(xiàn)傳遞參數(shù)"
? ? ? ? ? ? ? onPress={() =>
? ? ? ? ? ? ? ? this.props.navigation.navigate("Details", { name: "張三", age: 20 })
? ? ? ? ? ? ? }
? ? ? ? ? ? />
? ? ? ? 接收數(shù)據(jù)B:
? ? ? ? ? ? this.props.navigation.getParam("name", "admin")
? ? 4)配置標(biāo)簽欄
? ? ? //設(shè)置默認(rèn)標(biāo)題
? static navigationOptions = {
? title: "Home"
? };
? ? ? //設(shè)置參數(shù)作為標(biāo)題
? ? ? static navigationOptions = ({ navigation }) => {
? ? ? ? return {
? ? ? ? ? title: navigation.getParam("name", "Details")
? ? ? ? };
? ? ? };
? ? ? //單個(gè)頁(yè)面更改樣式
? ? ? {
? ? ? ? title: "Home",
? ? ? ? headerStyle: {
? ? ? ? ? backgroundColor: "#f4511e"
? ? ? ? },
? ? ? ? headerTintColor: "#605",
? ? ? ? headerTitleStyle: {
? ? ? ? ? fontWeight: "bold"
? ? ? ? }
? ? ? };
? ? ? //在路由表中配置樣式必指,實(shí)現(xiàn)多個(gè)頁(yè)面共享樣式
? ? ? const Route = createStackNavigator(
? ? ? ? {
? ? ? ? ? Home: {
? ? ? ? ? ? screen: Home
? ? ? ? ? },
? ? ? ? ? Details: Details
? ? ? ? },
? ? ? ? {
? ? ? ? ? initialRouteName: "Home",
? ? ? ? ? //跨頁(yè)面共享標(biāo)題欄設(shè)置
? ? ? ? ? navigationOptions: {
? ? ? ? ? ? headerStyle: {
? ? ? ? ? ? ? backgroundColor: "#f4511e"
? ? ? ? ? ? },
? ? ? ? ? ? headerTintColor: "#fff",
? ? ? ? ? ? headerTitleStyle: {
? ? ? ? ? ? ? fontWeight: "bold"
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }
? ? ? );
二棋嘲、頂部導(dǎo)航欄
? ? ? 1.案例使用:
? ? ? ? import { createTabNavigator } from "react-navigation";
? ? ? ? import JHSreeen from "./JHSreeen";
? ? ? ? import FXSreeen from "./FXSreeen";
? ? ? ? const TieScreen = createTabNavigator(
? ? ? ? ? {
? ? ? ? ? ? JH: {
? ? ? ? ? ? ? screen: JHSreeen,
? ? ? ? ? ? ? navigationOptions: {
? ? ? ? ? ? ? ? title: "精華"
? ? ? ? ? ? ? }
? ? ? ? ? ? },
? ? ? ? ? ? FX: {
? ? ? ? ? ? ? screen: FXSreeen,
? ? ? ? ? ? ? navigationOptions: {
? ? ? ? ? ? ? ? title: "分享"
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? );
? ? ? 2.頂部導(dǎo)航欄設(shè)置屬性:
? ? ? ? {
? ? ? ? ? //設(shè)置標(biāo)題欄通配效果
? ? ? ? ? navigationOptions: {
? ? ? ? ? ? title: "標(biāo)題"
? ? ? ? ? },
? ? ? ? ? //設(shè)置文本選中前后效果顏色
? ? ? ? ? tabBarOptions: {
? ? ? ? ? ? activeTintColor: "white", //激活樣式
? ? ? ? ? ? inactiveTintColor: "gray" //未激活樣式
? ? ? ? ? },
? ? ? ? ? tabBarPosition: "top", //設(shè)置顯示位置
? ? ? ? ? swipeEnabled: true, //是否可以滑動(dòng)
? ? ? ? ? animationEnabled: true //滑動(dòng)效果
? ? ? ? }
三鼓蜒、底部導(dǎo)航欄
? ? ? 1.案例使用:
? ? ? ? ? import { createBottomTabNavigator } from "react-navigation";
? ? ? ? ? import Ionicons from "react-native-vector-icons/Ionicons";
? ? ? ? ? const Home = createBottomTabNavigator(
? ? ? ? ? ? {
? ? ? ? ? ? ? Topic: {
? ? ? ? ? ? ? ? screen: Topic,
? ? ? ? ? ? ? ? navigationOptions: {
? ? ? ? ? ? ? ? ? title: "帖子"
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? },
? ? ? ? ? ? ? Publish: {
? ? ? ? ? ? ? ? screen: Publish,
? ? ? ? ? ? ? ? navigationOptions: {
? ? ? ? ? ? ? ? ? title: "發(fā)布"
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? },
? ? ? ? ? ? ? My: {
? ? ? ? ? ? ? ? screen: My,
? ? ? ? ? ? ? ? navigationOptions: {
? ? ? ? ? ? ? ? ? title: "我的"
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? );
? ? ? ? 2.底部導(dǎo)航欄設(shè)置屬性:
? ? ? ? ? ? 導(dǎo)入三方圖片庫(kù):yarn add react-native-vector-icons
? ? ? ? ? ? 引入關(guān)聯(lián):react-native link
? ? ? ? ? ? 導(dǎo)入依賴:
? ? ? ? ? ? ? ? import Ionicons from "react-native-vector-icons/Ionicons";
? ? ? ? ? ? ? ? import React, { Component } from "react";
? ? ? ? ? ? Android項(xiàng)目中做編譯桩盲,運(yùn)行(更新曲饱、更新趴拧、更改implementation)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //配置底部導(dǎo)航圖片
? ? ? ? ? ? ? ? navigationOptions: ({ navigation }) => ({
? ? ? ? ? ? ? ? ? tabBarIcon: ({ focused, tintColor }) => {
? ? ? ? ? ? ? ? ? ? const { routeName } = navigation.state;
? ? ? ? ? ? ? ? ? ? let iconName;
? ? ? ? ? ? ? ? ? ? if (routeName === "Tab1") {
? ? ? ? ? ? ? ? ? ? ? iconName = "ios-document";
? ? ? ? ? ? ? ? ? ? } else if (routeName === "Tab2") {
? ? ? ? ? ? ? ? ? ? ? iconName = "ios-create";
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return <Ionicons name={iconName} size={25} color={tintColor} />;
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? }
? ? ? ? 3.底部導(dǎo)航欄加載本地圖片
? ? ? ? ? ? {
? ? ? ? ? ? ? ? backBehavior: "none",
? ? ? ? ? ? ? ? tabBarOptions: {
? ? ? ? ? ? ? ? ? activeTintColor: "#5599ff",
? ? ? ? ? ? ? ? ? style: { backgroundColor: "#f8f8f8" },
? ? ? ? ? ? ? ? ? indicatorStyle: { opacity: 0 },
? ? ? ? ? ? ? ? ? tabStyle: { padding: 0 },
? ? ? ? ? ? ? ? ? labelStyle: { fontSize: 12 }
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? navigationOptions: ({ navigation }) => ({
? ? ? ? ? ? ? ? ? tabBarIcon: ({ focused, tintColor }) => {
? ? ? ? ? ? ? ? ? ? const { routeName } = navigation.state;
? ? ? ? ? ? ? ? ? ? if (routeName === "首頁(yè)") {
? ? ? ? ? ? ? ? ? ? ? if (focused) {
? ? ? ? ? ? ? ? ? ? ? ? return (
? ? ? ? ? ? ? ? ? ? ? ? ? <Image
? ? ? ? ? ? ? ? ? ? ? ? ? ? source={require("../Images/TabBar/tabBar_home_click_icon.png")}
? ? ? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? ? ? ? ? );
? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? return (
? ? ? ? ? ? ? ? ? ? ? ? ? <Image
? ? ? ? ? ? ? ? ? ? ? ? ? ? source={require("../Images/TabBar/tabBar_home_icon.png")}
? ? ? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? ? ? ? ? );
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? } else if (routeName === "消息") {
? ? ? ? ? ? ? ? ? ? ? if (focused) {
? ? ? ? ? ? ? ? ? ? ? ? return (
? ? ? ? ? ? ? ? ? ? ? ? ? <Image
? ? ? ? ? ? ? ? ? ? ? ? ? ? source={require("../Images/TabBar/tabBar_casehistory_click_icon.png")}
? ? ? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? ? ? ? ? );
? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? return (
? ? ? ? ? ? ? ? ? ? ? ? ? <Image
? ? ? ? ? ? ? ? ? ? ? ? ? ? source={require("../Images/TabBar/tabBar_casehistory_icon.png")}
? ? ? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? ? ? ? ? );
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? } else if (routeName === "我的") {
? ? ? ? ? ? ? ? ? ? ? if (focused) {
? ? ? ? ? ? ? ? ? ? ? ? return (
? ? ? ? ? ? ? ? ? ? ? ? ? <Image
? ? ? ? ? ? ? ? ? ? ? ? ? ? source={require("../Images/TabBar/tabBar_educat_click_icon.png")}
? ? ? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? ? ? ? ? );
? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? return (
? ? ? ? ? ? ? ? ? ? ? ? ? <Image
? ? ? ? ? ? ? ? ? ? ? ? ? ? source={require("../Images/TabBar/tabBar_educat_icon.png")}
? ? ? ? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? ? ? ? ? );
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? }
四、身份認(rèn)證
? ? ? Route.js? 路由頁(yè)面
? ? ? ? ? import { createSwitchNavigator } from "react-navigation";
? ? ? ? ? import Auth from "./Auth";
? ? ? ? ? import Home from "./Home";
? ? ? ? ? import Login from "./Login";
? ? ? ? ? const Route = createSwitchNavigator(
? ? ? ? ? ? {
? ? ? ? ? ? ? Auth: Auth,
? ? ? ? ? ? ? Home: Home,
? ? ? ? ? ? ? Login: Login
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? initialRouteName: "Auth"
? ? ? ? ? ? }
? ? ? ? ? );
? ? ? Auth.,js 認(rèn)證頁(yè)面(狀態(tài)判斷)
? ? ? ? //判斷登錄認(rèn)證狀態(tài):true:Home? false:Login
? ? ? ? _bootstrapAsync = async () => {
? ? ? ? ? //獲取Sp文件中保存的用戶信息懈费,用戶判斷用戶登錄的狀態(tài)
? ? ? ? ? const name = await AsyncStorage.getItem("name");
? ? ? ? ? if (name && name.length >= 6) {
? ? ? ? ? ? this.props.navigation.navigate("Home");
? ? ? ? ? } else {
? ? ? ? ? ? this.props.navigation.navigate("Login");
? ? ? ? ? }
? ? ? ? ? // const userToken = await AsyncStorage.getItem("userToken");
? ? ? ? ? // this.props.navigation.navigate(userToken ? "App" : "Auth");
? ? ? ? };
? ? ? Login.js? 登錄頁(yè)面(用戶登錄)
? ? ? ? _signInAsync = async () => {
? ? ? ? ? //保存登錄信息到SP文件
? ? ? ? ? await AsyncStorage.setItem("name", "abc123456");
? ? ? ? ? //登錄后跳轉(zhuǎn)到主頁(yè)面
? ? ? ? ? this.props.navigation.navigate("Home");
? ? ? ? };
? ? ? Home.js? 主頁(yè)面 (注銷功能)
? ? ? ? _signOutAsync = async () => {
? ? ? ? ? //將Sp文件中保存的用戶信息清空
? ? ? ? ? await AsyncStorage.clear();
? ? ? ? ? //跳轉(zhuǎn)到登錄頁(yè)面
? ? ? ? ? this.props.navigation.navigate("Login");
? ? ? ? };