• ReactNative 学习总结 个人梳理


    目录

    一、基本使用

    1、创建项目

    2、ReactNative的布局默认为flex布局,View标签不能直接写入文字,样式不会被继承, 单位不能加px,vh,vw等可以是百分比。

    3、快速获取屏幕尺寸,Dimensions

    4、StyleSheet的使用

    5、Alert的使用

    6、判断是哪个平台

    7、常用组件解释

    二、使用Mobx存放全局数据

    安装依赖:

    找到babel.config.js文件添加如下配置:

    新建mobx目录,新建index.js文件:

    组件中的使用:

    动态修改数据

    三、react-navigation搭建路由

    1、安装使用

    2、Stack导航

    3、BottomTab导航

    4、Drawer导航

    5、MaterialTopTab导航

    6、路由嵌套

    7、路由传参

    四、图标库的使用

    1、安装依赖

    2、将如下代码贴到,android/app/build.gradle文件最后,路径不能错!

    3、选择你中意的图标

    五、ReactNative架构

    1、现有架构

    2、新架构


    ReactNative官方文档:

    React Native 中文网 · 使用React来编写原生应用的框架

    ReactNative适用于APP的一套多开,适合大型项目的开发,安装环境时注意版本对应。

    一、基本使用

    1、创建项目

    npx react-native init myPro
    

    2、ReactNative的布局默认为flex布局,View标签不能直接写入文字,样式不会被继承, 单位不能加px,vh,vw等可以是百分比。

    3、快速获取屏幕尺寸,Dimensions

    flex布局见我的博客:Flex布局总结_Dragon Wu的博客-CSDN博客_flex布局总结

    案例代码:同样使用React的JSX的写法,但标签需要使用RN的标签

    1. import React from "react"
    2. import { View, Text, Dimensions } from "react-native"
    3. // 获取屏幕宽高
    4. const screenWidth = Math.round(Dimensions.get("window").width)
    5. const screenHeight = Math.round(Dimensions.get("window").height)
    6. /*
    7. 1 在rn中默认容器 布局方式 都是flex布局
    8. 2 方向 flex-direction:column;
    9. 3 rn中样式不会被继承
    10. 4 单位
    11. (1)不用加px
    12. (2)不用加vw vh
    13. (3)可以加 百分比 ”50%“
    14. */
    15. const Index = () => (
    16. <View style={{
    17. backgroundColor: "aqua",
    18. flex: 1,
    19. flexDirection: "row",
    20. color: "red",
    21. fontSize: 50,
    22. width: "50%"
    23. }}>
    24. <Text>JSXText>
    25. <Text style={{ color: "yellow", fontSize: 100 }}>JSXText>
    26. <View style={{ width: screenWidth / 2, height: screenHeight / 2, backgroundColor: "yellow" }}><Text>屏幕尺寸的一半Text>View>
    27. View>
    28. )
    29. export default Index

    效果如图。

    4、StyleSheet的使用

    5、Alert的使用

    1. import React, { Component } from "react"
    2. import { Alert, Button, View, StyleSheet } from "react-native"
    3. class App extends Component {
    4. createTwoButtonAlert = () => {
    5. Alert.alert(
    6. "警告标题",
    7. "警告内容",
    8. [
    9. {
    10. text: "取消",
    11. onPress: () => console.log("Cancel"),
    12. style: "cancel"
    13. },
    14. {
    15. text: "确认",
    16. onPress: () => console.log("OK"),
    17. style: "default"
    18. }
    19. ]
    20. )
    21. }
    22. render () {
    23. return (
    24. <View style={[styles.container]}>
    25. <Button title="alert"
    26. color={"red"}
    27. onPress={() => {
    28. alert("我是一个按钮")
    29. }} />
    30. <Button title={"Alert.alert"} onPress={() => {
    31. Alert.alert("我是一个按钮")
    32. }}
    33. color={"blue"}
    34. />
    35. <Button title={"两个按钮"}
    36. onPress={this.createTwoButtonAlert}
    37. color={"green"} />
    38. View>
    39. )
    40. }
    41. }
    42. export default App
    43. const styles = StyleSheet.create({
    44. container: {
    45. flex: 1,
    46. justifyContent: "space-around",
    47. alignItems: "center"
    48. }
    49. })

    alert如下:

     Alert.alert效果如下:

     Alert.alert的选择用法:

    6、判断是哪个平台

    7、常用组件解释

    组件名作用
    StatusBar可以用于修改手机顶部状态栏的显示,hidden=true时隐藏状态栏
    Siwtch用于控件状态
    ActivityIndicator加载时的圆形加载条
    Touchable点击时有样式改变效果,类似于按钮
    ScrollView可滚动的视图,View是不能滚动的,但这个可以。可设置水平滚动或垂直滚动。可作为导航栏。
    SafeAreaView可以保证视图存于合理的视图区域,可避免某些手机刘海屏对视图的遮挡
    SetionList可以进行分组的列表
    FlatList支持水平布局的列表

     第三方组件需要先下载依赖才能使用。

    二、使用Mobx存放全局数据

    大型项目建议使用Redux

    官方文档:2. API概览 · MobX 中文文档

    安装依赖:

    yarn add mobx mobx-react @babel/plugin-proposal-decorators
    

    找到babel.config.js文件添加如下配置:

    1. module.exports = {
    2. plugins: [
    3. ["@babel/plugin-proposal-decorators", { "legacy": true }]
    4. ]
    5. }

    新建mobx目录,新建index.js文件:

    1. import {observable} from "mobx"
    2. class RootStore{
    3. //es7的装饰器语法 Object.defineProperty
    4. @observable
    5. name="悟空"
    6. }
    7. export default new RootStore()

    组件中的使用:

     父组件传入全局变量:

    1. import React, { Component } from "react"
    2. import { View, Text } from "react-native"
    3. import RootStore from "./mobx"
    4. import { Provider } from "mobx-react"
    5. import Btn from "./component/Btn"
    6. class Index extends Component {
    7. render () {
    8. return (
    9. <View>
    10. <Text>Text>
    11. <Provider RootStore={RootStore}>
    12. {/* 里面的组件可以拿到全局数据*/}
    13. <Btn/>
    14. Provider>
    15. View>
    16. )
    17. }
    18. }
    19. export default Index

    子组件接收全局变量:

    1. import React, { Component } from "react"
    2. import {View,Text} from "react-native"
    3. import { inject } from "mobx-react"
    4. @inject("RootStore")
    5. class Btn extends Component {
    6. render () {
    7. return (
    8. <View>
    9. <Text>Btn:{this.props.RootStore.name}Text>
    10. View>
    11. )
    12. }
    13. }
    14. export default Btn

    效果:获取到全局变量的数据

    动态修改数据

    点击以后动态变换:这里注意高版本的mobx需要使用makeObservable才能实现页面的动态渲染。

    三、react-navigation搭建路由

    yarn add @react-navigation/native@^5.x

    官方文档:https://reactnavigation.org/docs/getting-started

    这里的案例为稳定版本:5.x

    1、安装使用

    参考:

    https://reactnavigation.org/docs/5.x/getting-started

    下载依赖:

    yarn add @react-navigation/native@^5.x
    yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view

    注意5.x版本使用前要在入口文件index.js或app.js引入如下:

    import 'react-native-gesture-handler';

    2、Stack导航

    安装依赖:

    yarn add @react-navigation/stack@^5.x

    参考:

    https://reactnavigation.org/docs/5.x/hello-react-navigation

    案例代码:

    1. import { createNativeStackNavigator } from "react-native-screens/native-stack"
    2. import { NavigationContainer } from "@react-navigation/native"
    3. import { Button, Text, TouchableOpacity } from "react-native"
    4. import * as React from "react"
    5. function HomeScreen (prop) {
    6. return (<Button title={"跳转到详情页"} onPress={() => prop.navigation.navigate("About")} />)
    7. }
    8. function AboutScreen ({ navigation }) {
    9. return (<Button title={"跳转到主页"} onPress={() => navigation.navigate("Home")} />)
    10. }
    11. const Stack = createNativeStackNavigator()
    12. function App () {
    13. return (
    14. <NavigationContainer>
    15. <Stack.Navigator>
    16. <Stack.Screen
    17. options={{
    18. title: "首页",
    19. headerStyle: {
    20. backgroundColor: "tomato"
    21. },
    22. headerRight: () => (
    23. <TouchableOpacity onPress={() => alert("hello")}>
    24. <Text>helloText>
    25. TouchableOpacity>
    26. )
    27. }}
    28. name="Home" component={HomeScreen} />
    29. <Stack.Screen name="About" component={AboutScreen} />
    30. Stack.Navigator>
    31. NavigationContainer>
    32. )
    33. }
    34. export default App

    效果:

     

    3、BottomTab导航

    安装依赖:

    yarn add @react-navigation/bottom-tabs@^5.x

     参考:

    https://reactnavigation.org/docs/5.x/tab-based-navigation

    导航栏使用方法基本相同,先添加依赖,之后导入navigator生成器,进行路由。 

    基本实现:

    1. import * as React from 'react';
    2. import { Text, View } from 'react-native';
    3. import { NavigationContainer } from '@react-navigation/native';
    4. import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
    5. function HomeScreen() {
    6. return (
    7. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
    8. <Text>Home!Text>
    9. View>
    10. );
    11. }
    12. function SettingsScreen() {
    13. return (
    14. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
    15. <Text>Settings!Text>
    16. View>
    17. );
    18. }
    19. const Tab = createBottomTabNavigator();
    20. export default function App() {
    21. return (
    22. <NavigationContainer>
    23. <Tab.Navigator>
    24. <Tab.Screen name="Home" component={HomeScreen} />
    25. <Tab.Screen name="Settings" component={SettingsScreen} />
    26. Tab.Navigator>
    27. NavigationContainer>
    28. );
    29. }

    效果:

    需要看图标使用的见 (四、图标库的使用)

    4、Drawer导航

    使用与前两种类似:

    首先,添加依赖:

    yarn add @react-navigation/drawer@^5.x

    参考:

    https://reactnavigation.org/docs/5.x/drawer-based-navigation

     

     

    参考:https://reactnavigation.org/docs/5.x/drawer-navigator 

    案例代码:

    1. import * as React from "react"
    2. import { Button, View } from "react-native"
    3. import { createDrawerNavigator } from "@react-navigation/drawer"
    4. import { NavigationContainer } from "@react-navigation/native"
    5. function HomeScreen ({ navigation }) {
    6. return (
    7. <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
    8. <Button
    9. onPress={() => navigation.navigate("Notifications")}
    10. title="Go to notifications"
    11. />
    12. View>
    13. )
    14. }
    15. function NotificationsScreen ({ navigation }) {
    16. return (
    17. <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
    18. <Button onPress={() => navigation.navigate("Home")} title="Go back home" />
    19. View>
    20. )
    21. }
    22. const Drawer = createDrawerNavigator()
    23. export default function App () {
    24. return (
    25. <NavigationContainer>
    26. <Drawer.Navigator>
    27. <Drawer.Screen name="Home" component={HomeScreen} />
    28. <Drawer.Screen name="Notifications" component={NotificationsScreen} />
    29. Drawer.Navigator>
    30. NavigationContainer>
    31. )
    32. }

    效果截图:

     注,若启动或按键有问题,试试清清缓存,也可以参考这个:

    Installation | React Native Reanimated

    重新启动一般就可以刷新了。

     5、MaterialTopTab导航

    参考:https://reactnavigation.org/docs/5.x/material-top-tab-navigator

    添加依赖:

    yarn add @react-navigation/material-top-tabs@^5.x react-native-tab-view@^2.x

    案例代码:相比于TabView多了一个滑动切换功能。

    1. import * as React from "react"
    2. import { Text, View } from "react-native"
    3. import { NavigationContainer } from "@react-navigation/native"
    4. import { createMaterialTopTabNavigator } from "@react-navigation/material-top-tabs"
    5. function HomeScreen () {
    6. return (
    7. <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
    8. <Text>Home!Text>
    9. View>
    10. )
    11. }
    12. function SettingsScreen () {
    13. return (
    14. <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
    15. <Text>Settings!Text>
    16. View>
    17. )
    18. }
    19. const Tab = createMaterialTopTabNavigator()
    20. export default function App () {
    21. return (
    22. <NavigationContainer>
    23. <Tab.Navigator>
    24. <Tab.Screen name="Home" component={HomeScreen} />
    25. <Tab.Screen name="Settings" component={SettingsScreen} />
    26. Tab.Navigator>
    27. NavigationContainer>
    28. )
    29. }

    效果:

     注意,使用时可能因为

    react-native-reanimated

    报错!

    解决方法见:Installation | React Native Reanimated

    6、路由嵌套

    参考:https://reactnavigation.org/docs/5.x/nesting-navigators

    案例代码:

    1. // In App.js in a new project
    2. import * as React from 'react';
    3. import { NavigationContainer } from '@react-navigation/native';
    4. import { createStackNavigator } from '@react-navigation/stack';
    5. import { createBottomTabNavigator } from "@react-navigation/bottom-tabs"
    6. import { Text, View } from "react-native"
    7. function Feed(prop){
    8. return (
    9. <View>
    10. <Text>FeedText>
    11. View>
    12. )
    13. }
    14. function Messages(prop){
    15. return (
    16. <View>
    17. <Text>MessagesText>
    18. View>
    19. )
    20. }
    21. function HomeScreen() {
    22. const Tab=createBottomTabNavigator()
    23. return (
    24. <Tab.Navigator>
    25. <Tab.Screen name="Feed" component={Feed} />
    26. <Tab.Screen name="Messages" component={Messages} />
    27. Tab.Navigator>
    28. );
    29. }
    30. const Stack = createStackNavigator();
    31. function App() {
    32. return (
    33. <NavigationContainer>
    34. <Stack.Navigator>
    35. <Stack.Screen name="Home" component={HomeScreen} />
    36. Stack.Navigator>
    37. NavigationContainer>
    38. );
    39. }
    40. export default App;

    效果:

     7、路由传参

     参考:https://reactnavigation.org/docs/5.x/params

    案例代码:

    1. import * as React from 'react';
    2. import { Button, Text, View } from "react-native"
    3. import { NavigationContainer } from '@react-navigation/native'
    4. import { createStackNavigator } from "@react-navigation/stack"
    5. function HomeScreen({ navigation }) {
    6. return (
    7. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
    8. <Text>Home ScreenText>
    9. <Button
    10. title="Go to Details"
    11. onPress={() => {
    12. /* 1. Navigate to the Details route with params */
    13. navigation.navigate('Details', {
    14. itemId: 86,
    15. otherParam: 'anything you want here',
    16. });
    17. }}
    18. />
    19. View>
    20. );
    21. }
    22. function DetailsScreen({ route, navigation }) {
    23. /* 2. Get the param */
    24. const { itemId, otherParam } = route.params;
    25. return (
    26. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
    27. <Text>Details ScreenText>
    28. <Text>itemId: {JSON.stringify(itemId)}Text>
    29. <Text>otherParam: {JSON.stringify(otherParam)}Text>
    30. <Button
    31. title="Go to Details... again"
    32. onPress={() =>
    33. navigation.push('Details', {
    34. itemId: Math.floor(Math.random() * 100),
    35. })
    36. }
    37. />
    38. <Button title="Go to Home" onPress={() => navigation.navigate('Home')} />
    39. <Button title="Go back" onPress={() => navigation.goBack()} />
    40. View>
    41. );
    42. }
    43. const Stack = createStackNavigator();
    44. export default function App() {
    45. return (
    46. <NavigationContainer>
    47. <Stack.Navigator>
    48. <Stack.Screen name="Home" component={HomeScreen} />
    49. <Stack.Screen name="Details" component={DetailsScreen} />
    50. Stack.Navigator>
    51. NavigationContainer>
    52. );
    53. }

    效果:

    四、图标库的使用

    图标仓库链接:GitHub - oblador/react-native-vector-icons: Customizable Icons for React Native with support for image source and full styling.

    Ios见上面仓库链接

    1、安装依赖

    yarn add react-native-vector-icons

     以下为Android环境演示:

    2、将如下代码贴到,android/app/build.gradle文件最后,路径不能错!

    apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"

    3、选择你中意的图标

    可以是这个:Ionicons: Premium Open Source Icon Pack for Ionic Framework

     引入对应的依赖:

    import Ionicons from 'react-native-vector-icons/Ionicons';

    Tab路由的使用案例:

    1. import * as React from 'react';
    2. import { Text, View } from 'react-native';
    3. import { NavigationContainer } from '@react-navigation/native';
    4. import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
    5. import Ionicons from 'react-native-vector-icons/Ionicons';
    6. function HomeScreen() {
    7. return (
    8. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
    9. <Text>Home!Text>
    10. View>
    11. );
    12. }
    13. function SettingsScreen() {
    14. return (
    15. <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
    16. <Text>Settings!Text>
    17. View>
    18. );
    19. }
    20. const Tab = createBottomTabNavigator();
    21. export default function App() {
    22. return (
    23. <NavigationContainer>
    24. <Tab.Navigator
    25. screenOptions={({ route }) => ({
    26. tabBarIcon: ({ focused, color, size }) => {
    27. let iconName;
    28. if (route.name === 'Home') {
    29. iconName = focused
    30. ? 'ios-information-circle'
    31. : 'ios-information-circle-outline';
    32. } else if (route.name === 'Settings') {
    33. iconName = focused ? 'ios-list-box' : 'ios-list';
    34. }
    35. // You can return any component that you like here!
    36. return <Ionicons name={iconName} size={size} color={color} />;
    37. },
    38. })}
    39. tabBarOptions={{
    40. activeTintColor: 'tomato',
    41. inactiveTintColor: 'gray',
    42. }}
    43. >
    44. <Tab.Screen name="Home" component={HomeScreen} />
    45. <Tab.Screen name="Settings" component={SettingsScreen} />
    46. Tab.Navigator>
    47. NavigationContainer>
    48. );
    49. }

    效果:

    五、ReactNative架构

    1、现有架构

     2、新架构

    总结到此,后续补充。

  • 相关阅读:
    SEO与社交媒体的关系
    2023年天津市大学软件学院高职升本科联合招生专业考试大纲
    一次对requirements环境的配置
    算法--判断矩阵经轮转后是否一致
    线上线程池配置错误导致服务故障
    经典算法之直接插入排序(DirectInsert Sort)
    蛋白组学治疗时点及不同治疗方式疗效差异比较
    String的深入认识
    PoE交换机出现不稳定的原因有哪些?
    c++ mutable
  • 原文地址:https://blog.csdn.net/qq_50909707/article/details/127788413