[快速上手RN] 0.React Native 快速启动项目
[快速上手RN] 1. React native 项目集成UI Kitten
[快速上手RN] 2. React native 项目色彩主题色编辑及使用
[快速上手RN] 3. React native 制作底部导航栏 Bottom Tab Bar
[快速上手RN] 4. React native 集成redux
首先我们确认目标
我们所需要的依赖如下
安装命令如下,如果觉得麻烦可以复制粘贴~
npm install react-navigation @react-navigation/native @react-navigation/native-stack @react-navigation/bottom-tabs --save
npm install react-native-safe-area-context --save
具体请查看博文
[快速上手RN] 1. React native 项目集成UI Kitten
这里暂不赘述
如果你的项目是通过Expo启动的,那么直接使用即可,否则也可以使用自己的Icon库或者试试好用的Expo来开发~
参考博文
[快速上手RN] 0.React Native 快速启动项目
src
文件夹中创建导航栏文件夹navigator
src/navigator/
下创建2个文件AppNavigator.jsx
用于export项目的整个导航TabNavigation.jsx
用于编写底部导航栏的相关代码以下为该文件代码,根据react-navigation的文档,我们需要做一个container包住navigator,并导出以便App.js使用
需要import TabNavigation
import { NavigationContainer } from "@react-navigation/native";
import TabNavigatorScreen from "./TabNavigation";
export default function () {
return (
<NavigationContainer>
<TabNavigatorScreen />
</NavigationContainer>
);
}
打开TabNavigation.jsx
import React from "react";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import {
BottomNavigation,
BottomNavigationTab,
Layout,
Text,
} from "@ui-kitten/components"; // UI Kitten的组件
import { MaterialIcons } from "@expo/vector-icons"; // 用于Icon
我们需要创建页面组件,“首页”和“我的”,如果你已经有自己的页面了,那么直接使用!
const HomeScreen = (props) => {
return (
<Layout style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Home Screen</Text>
</Layout>
);
};
Tips:
const HomeStack = createNativeStackNavigator();
const HomeStackScreen = () => {
return (
<HomeStack.Navigator
initialRouteName="Home"
screenOptions={{
headerShown: false,
}}
>
<HomeStack.Screen name="Home" component={HomeScreen} />
</HomeStack.Navigator>
);
};
Code
const AccountScreen = (props) => {
return (
<Layout style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Account Screen</Text>
</Layout>
);
};
const AccountStack = createNativeStackNavigator();
const AccountStackScreen = () => {
return (
<AccountStack.Navigator
initialRouteName="Account"
screenOptions={{
headerShown: false,
}}
>
<AccountStack.Group>
<AccountStack.Screen name="Account" component={AccountScreen} />
</AccountStack.Group>
</AccountStack.Navigator>
);
};
Tips
appearance="noIndicator"
指定不需要指示线,更多api请查阅UI Kitten的bottom-tabs apiCode
const BottomTabBar = ({ navigation, state }) => (
<BottomNavigation
selectedIndex={state.index}
appearance="noIndicator"
onSelect={(index) => navigation.navigate(state.routeNames[index])}
>
<BottomNavigationTab
title="Home"
icon={(props) => (
<MaterialIcons
name="home"
size={24}
color={props.style.tintColor}
/>
)}
/>
<BottomNavigationTab
title="Account"
icon={(props) => (
<MaterialIcons
name="account-circle"
size={24}
color={props.style.tintColor}
/>
)}
/>
</BottomNavigation>
);
Code
const TabNavigator = createBottomTabNavigator();
const TabNavigatorScreen = () => {
return (
<TabNavigator.Navigator tabBar={(props) => <BottomTabBar {...props} />}>
<TabNavigator.Screen name="HomeStackScreen" component={HomeStackScreen} />
<TabNavigator.Screen
name="AccountStackScreen"
component={AccountStackScreen}
/>
</TabNavigator.Navigator>
);
};
export default TabNavigatorScreen;
import React from "react";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import {
BottomNavigation,
BottomNavigationTab,
Layout,
Text,
} from "@ui-kitten/components";
import { MaterialIcons } from "@expo/vector-icons";
// screens
// import AccountScreen from "../screens/Account";
const HomeScreen = (props) => {
return (
<Layout style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Home Screen</Text>
</Layout>
);
};
const HomeStack = createNativeStackNavigator();
const HomeStackScreen = () => {
return (
<HomeStack.Navigator
initialRouteName="Home"
screenOptions={{
headerShown: false,
}}
>
<HomeStack.Screen name="Home" component={HomeScreen} />
</HomeStack.Navigator>
);
};
const AccountScreen = (props) => {
return (
<Layout style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Account Screen</Text>
</Layout>
);
};
const AccountStack = createNativeStackNavigator();
const AccountStackScreen = () => {
return (
<AccountStack.Navigator
initialRouteName="Account"
screenOptions={{
headerShown: false,
}}
>
<AccountStack.Group>
<AccountStack.Screen name="Account" component={AccountScreen} />
</AccountStack.Group>
</AccountStack.Navigator>
);
};
const BottomTabBar = ({ navigation, state }) => (
<BottomNavigation
selectedIndex={state.index}
appearance="noIndicator"
onSelect={(index) => navigation.navigate(state.routeNames[index])}
>
<BottomNavigationTab
title="Home"
icon={(props) => (
<MaterialIcons
name="home"
size={24}
color={props.style.tintColor}
/>
)}
/>
<BottomNavigationTab
title="Account"
icon={(props) => (
<MaterialIcons
name="account-circle"
size={24}
color={props.style.tintColor}
/>
)}
/>
</BottomNavigation>
);
const TabNavigator = createBottomTabNavigator();
const TabNavigatorScreen = () => {
return (
<TabNavigator.Navigator tabBar={(props) => <BottomTabBar {...props} />}>
<TabNavigator.Screen name="HomeStackScreen" component={HomeStackScreen} />
<TabNavigator.Screen
name="AccountStackScreen"
component={AccountStackScreen}
/>
</TabNavigator.Navigator>
);
};
export default TabNavigatorScreen;
import React from "react";
import * as eva from "@eva-design/eva";
import { ApplicationProvider } from "@ui-kitten/components"; // Provider
import AppNavigator from "./src/navigator/AppNavigator"; // 导航
import { default as theme } from "./src/color/custom-theme.json"; // 如果没制作主题文件,可参考上一篇博文,或删除该段代码以及下方的theme变量使用
import { SafeAreaView } from "react-native-safe-area-context"; // 安全边界
const App = () => {
return (
<ApplicationProvider {...eva} theme={{ ...eva.light, ...theme }}>
<SafeAreaView style={{ flex: 1 }}>
<AppNavigator />
</SafeAreaView>
</ApplicationProvider>
);
};
export default App;
完成!如有帮助请关注点赞~