关于React native Demo项目的总结

简述

至于项目都是学习其他人的实战项目,这里不过是做一个学习总结,主要是记录下使用到的包和一些UI工具,以便于参考。

  1. react-navigation 页面跳转和转场动画
  2. react-native-svg-uri RN 中使用svg技术
  3. react-native-tab-navigator 底部导航栏
  4. react-native-element 一套ui库 内置常用组件
  5. react-native-vector-icons
  6. react-native-linear-gradient 渐变容器
  7. react-native-confirmation-code-field 验证码输入框
  8. react-native-svg
  9. iconfont字体图标
  10. react-native-datepicker 日期选择框
  11. react-native-amap-geolocation 高德地图组件
  12. react-native-picker 自定义picker
  13. react-native-image-crop-picker 图片裁切组件
  14. jmessage-react-plugin 极光推送 react-native 版本
  15. react-native-image-header-scroll-view 顶部吸顶效果
  16. react-native-deck-swiper 类似轮播图的滑动组件
  17. react-native-image-zoom-viewer 图片缩放组件
  18. aurora-imui-react-native 通用的即时通讯(IM)UI 库
  19. react-native-scrollable-tab-view 顶部tab栏
  20. moment 流行的日期库
  21. react-native-image-picker 图片选择工具
  22. mobx mobx-react 状态管理工具
一、初始化创建

1、项目的搭建可以参考RN官网,这里使用的开发环境 Windows Android VsCode 雷电模拟器。

npx react-native init test

2、在运行项目之前先开启模拟器,使用adb 监测设备 具体参考adb

 adb device

3、运行项目

npm run android
二、使用的库

react-navigation

页面跳转和转场动画

  1. 安装

    yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view  @react-navigation/stack @react-navigation/native
    
  2. 代码

import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator, HeaderTitle } from '@react-navigation/stack';
const Stack = createStackNavigator();
render() {
        const {initialRouteName}=this.state;
        return (
            <NavigationContainer>
                <Stack.Navigator headerMode="none" initialRouteName={initialRouteName}>
                    <Stack.Screen name='home' component={MenuBar} />
                    <Stack.Screen name='login' component={LoginPage} />
                </Stack.Navigator>
               
            </NavigationContainer>
        )
    }

react-native-svg-uri

RN 中使用svg技术

  1. 下载

    yarn  add  react-native-svg-uri react-native-svg
    
  2. 代码

import SvgUri from 'react-native-svg-uri';
 render() {
       return (
         <View>
           <SvgUri width="23" height="23" svgXmlData={'<svg t="1568188030646"  viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7085" width="64" height="64"><path d="M515.3 597.2c-72.6 0-140.8-28.3-192.2-79.6-51.3-51.3-79.6-119.6-79.6-192.2s28.3-140.8 79.6-192.2 119.6-79.6 192.2-79.6 140.8 28.3 192.2 79.6 79.6 119.6 79.6 192.2-28.3 140.8-79.6 192.2c-51.4 51.3-119.6 79.6-192.2 79.6z m0-503.5c-61.9 0-120.1 24.1-163.9 67.9s-67.9 102-67.9 163.9 24.1 120.1 67.9 163.9c43.8 43.8 102 67.9 163.9 67.9 61.9 0 120.1-24.1 163.9-67.9 43.8-43.8 67.9-102 67.9-163.9 0-61.9-24.1-120.1-67.9-163.9-43.8-43.8-102-67.9-163.9-67.9zM994.9 915.6H62.3l3.8-23.3c12.4-75 51.3-143.9 109.6-194 59-50.6 134-78.5 211.3-78.5h283.1c77.3 0 152.3 27.9 211.3 78.5 58.3 50 97.2 118.9 109.6 194l3.9 23.3z m-884.4-40h836.2c-14.4-56.7-46.2-108.2-91.3-146.9-51.7-44.3-117.5-68.7-185.2-68.7H387c-67.7 0-133.5 24.4-185.2 68.8-45.1 38.7-77 90.2-91.3 146.8z" p-id="7086" fill="#999999"></path><path d="M448.1 640L513 841l80-201zM437.7 345c-12.1 0-21.9-9.9-21.9-21.9V269c0-12.1 9.9-21.9 21.9-21.9 12.1 0 21.9 9.9 21.9 21.9v54.1c0.1 12-9.8 21.9-21.9 21.9zM573.2 345c-12.1 0-21.9-9.9-21.9-21.9V269c0-12.1 9.9-21.9 21.9-21.9 12.1 0 21.9 9.9 21.9 21.9v54.1c0.1 12-9.8 21.9-21.9 21.9z" p-id="7087" fill="#999999" ></path></svg>'} />
         </View>
       );
     }

react-native-tab-navigator

底部导航栏

  1. 下载

    yarn add react-native-tab-navigator
    

react-native-element

一套ui库 内置常用组件

  1. 下载

    需要使用到图标 因此也需要安装 react-native-vector-icons

    yarn add react-native-elements react-native-vector-icons
    
  2. 引入和使用

    import { Icon } from 'react-native-elements'
    
    <Icon
      name='rowing' />
    

react-native-vector-icons 的其他使用

  1. 编辑 android/app/build.gradle

  2. 添加以下配置

    project.ext.vectoricons = [
        iconFontNames: [ 'MaterialIcons.ttf', 'EvilIcons.ttf' ] // Name of the font files you want to copy
    ]
    
    apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
    
  3. 重启项目

  4. 添加代码 如

    import FontAwesome5 from 'react-native-vector-icons/FontAwesome5';
    
    const icon = <FontAwesome5 name={'comments'} />;
    

react-native-linear-gradient

渐变容器

  1. 下载
yarn add react-native-linear-gradient
  1. 使用
<LinearGradient start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }} colors={['#9b63cd', '#e0708c']} style={styles.linearGradient}>
          <Text style={{ ...styles.buttonText, ...this.props.textStyle }}>{this.props.children}</Text>
        </LinearGradient>

react-native-confirmation-code-field

验证码输入框

  1. 下载

    yarn add react-native-confirmation-code-field
    
  2. 使用

import {CodeField,Cursor} from 'react-native-confirmation-code-field'

<CodeField
     value={codeTxt}
     onChangeText={this.onCodeChange}
     onSubmitEditing={this.onCodeSubmit}
     cellCount={6}
     rootStyle={styles.codeFiledRoot}
     keyboardType="number-pad"
     renderCell={({ index, symbol, isFocused }) => (
         <Text key={index} style={[styles.cell, isFocused && styles.focusCell]} >{symbol || (isFocused ? <Cursor /> : null)}</Text>
     )}
 />

react-native-svg

因为在rn中 使用普通的字体图标是没有多种颜色的如

  1. 下载依赖

    yarn add react-native-svg react-native-svg-uri
    
  2. 代码中使用

import SvgUri from 'react-native-svg-uri';
const female='<svg ......';

 <SvgUri width="23" height="23" svgXmlData={female} />

iconfont字体图标

  1. 在字体图标网站上下载 字体

  2. 然后拷贝 ttf后缀的文件到 android\app\src\main\assets\fonts中 如果没有assets文件夹可以新建一个

  3. 然后 给 Text 标签 设置

     <Text style={{ fontFamily: "iconfont", color: "red" }} >{'\ue82b'}</Text>
    

react-native-datepicker

日期选择框

  1. 安装

    yarn add  react-native-datepicker 
    
  2. 引入

    import DatePicker from 'react-native-datepicker';
    
  3. 使用

<DatePicker
   androidMode="spinner"
   style={{ width: "100%" }}
   date={birthday}
   mode="date"
   placeholder="设置生日"
   format="YYYY-MM-DD"
   minDate="1900-01-01"
   maxDate={currentDate}
   confirmBtnText="确定"
   cancelBtnText="取消"
   customStyles={{
     dateIcon: {
       display: "none"
     },
     dateInput: {
       marginLeft: pxToDp(10),
       borderWidth: 0,
       borderBottomWidth: pxToDp(1.1),
       alignItems: "flex-start",
       paddingLeft: pxToDp(4)
     },
     placeholderText: {
       fontSize: pxToDp(18),
       color: "#afafaf"
     }

   }}
   onDateChange={(birthday) => { this.setState({ birthday }) }}
 />

使用 react-native-amap-geolocation

高德地图组件

分别使用了两个功能,一个是AndroidSDK和一个web服务

  1. 申请 高度地图的key

  2. 下载依赖

    yarn add  react-native-amap-geolocation
    
  3. 配置文件

    1. 编辑 android/settings.gradle,设置项目路径:

      + include ':react-native-amap-geolocation'
      + project(':react-native-amap-geolocation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-amap-geolocation/lib/android')
      
    2. 编辑 android/app/build.gradle,新增依赖:

      dependencies {
      +   implementation project(':react-native-amap-geolocation')
      }
      
    3. 编辑 MainApplication.java

      + import cn.qiuxiang.react.geolocation.AMapGeolocationPackage;
      
      public class MainApplication extends Application implements ReactApplication {
        @Override
              protected List<ReactPackage> getPackages() {
                @SuppressWarnings("UnnecessaryLocalVariable")
                List<ReactPackage> packages = new PackageList(this).getPackages();
                // Packages that cannot be autolinked yet can be added manually here, for example:
      +         packages.add(new AMapGeolocationPackage());
                return packages;
              }
      }
      

react-native-picker

自定义picker

  1. 安装

    yarn add react-native-picker
    
  2. 代码

    import Picker from 'react-native-picker';
        Picker.init({
          pickerData: CityJson,
          selectedValue: ["北京", "北京"],
          wheelFlex: [1, 1, 0], // 显示省和市
          pickerConfirmBtnText: "确定",
          pickerCancelBtnText: "取消",
          pickerTitleText: "选择城市",
          onPickerConfirm: data => {
            // data =  [广东,广州,天河]
            this.setState(
              {
                city: data[1]
              }
            );
          }
        });
        Picker.show();
    

使用 react-native-image-crop-picker

图片裁切组件

  1. 安装

    yarn add  react-native-image-crop-picker 
    
  2. 使用

    import ImagePicker from 'react-native-image-crop-picker';
    
        ImagePicker.openPicker({
          width: 300,
          height: 400,
          cropping: true
        }).then(image => {
          console.log(image);
        });
    

jmessage-react-plugin

极光推送 react-native 版本

开通服务

  1. 首先 需要我们自己先在极光上注册账号 开通服务,拿到对应的密钥

简单使用

  1. 安装依赖

    yarn add jmessage-react-plugin jcore-react-native
    
  2. 配置

    1. android\app\src\main\AndroidManifest.xml 加入以下代码

            <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
            <!-- 极光的配置 -->
            <meta-data android:name="JPUSH_CHANNEL" android:value="${APP_CHANNEL}" />
            <meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}" />
            <!-- 极光的配置 -->
          </application>
      
    2. android\app\build.gradle 加入以下代码和按需修改

      android {
          compileSdkVersion rootProject.ext.compileSdkVersion
      
          compileOptions {
              sourceCompatibility JavaVersion.VERSION_1_8
              targetCompatibility JavaVersion.VERSION_1_8
          }
      
          defaultConfig {
              applicationId "com.awesomeproject22"
              minSdkVersion rootProject.ext.minSdkVersion
              targetSdkVersion rootProject.ext.targetSdkVersion
              versionCode 1
              versionName "1.0"
              multiDexEnabled true // 新增的
              manifestPlaceholders = [
              JPUSH_APPKEY: "c0c08d3d8babc318fe25bb0c",	//在此替换你的APPKey
              APP_CHANNEL: "developer-default"		//应用渠道号
              ]
          }
      

      dependencies {
          implementation fileTree(dir: "libs", include: ["*.jar"])
          implementation "com.facebook.react:react-native:+"  // From node_modules
          compile project(':jmessage-react-plugin') // 新增的
          compile project(':jcore-react-native')  // 新增的
          if (enableHermes) {
              def hermesPath = "../../node_modules/hermes-engine/android/";
              debugImplementation files(hermesPath + "hermes-debug.aar")
              releaseImplementation files(hermesPath + "hermes-release.aar")
          } else {
              implementation jscFlavor
          }
      }
      
    3. 根目录下新建文件和添加以下配置 react-native.config.js

      module.exports = {
        dependencies: {
          'jmessage-react-plugin': {
            platforms: {
              android: {
                packageInstance: 'new JMessageReactPackage(false)'
              }
            }
          },
        }
      };
      
    4. android\settings.gradle 加入如下配置

    include ':jmessage-react-plugin'
    project(':jmessage-react-plugin').projectDir = new File(rootProject.projectDir, '../node_modules/jmessage-react-plugin/android')
    include ':jcore-react-native'
    project(':jcore-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jcore-react-native/android')
    

react-native-image-header-scroll-view

顶部吸顶效果

  1. 下载

    yarn add react-native-image-header-scroll-view 
    

react-native-deck-swiper

类似轮播图的滑动组件

  1. 下载

    yarn add react-native-view-overflow  react-native-deck-swiper
    

react-native-image-zoom-viewer

图片缩放组件

  1. 下载

    yarn add react-native-image-zoom-viewer
    

aurora-imui-react-native

Aurora IMUI 是个通用的即时通讯(IM)UI 库,不特定于任何 IM SDK。

本 UI 库提供了消息列表、输入视图等常用组件,支持常见的消息类型:文字、图片、语音、视频等。默认包含多套界面风格,也能根据自己的需要自定义。

  1. 安装

    yarn  add  aurora-imui-react-native  react-native-fs
    
  2. 配置 引入 Package

    MainApplication.java

    import cn.jiguang.imui.messagelist.ReactIMUIPackage; // 新增的
    
    @Override
            protected List<ReactPackage> getPackages() {
              @SuppressWarnings("UnnecessaryLocalVariable")
              List<ReactPackage> packages = new PackageList(this).getPackages();
               packages.add(new AMapGeolocationPackage());
               packages.add(new ReactIMUIPackage()); // 新增的
              return packages;
            }
    
  3. android\settings.gradle 添加配置

    include ':app', ':aurora-imui-react-native'
    project(':aurora-imui-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/aurora-imui-react-native/ReactNative/android')
    
  4. android\app\build.gradle 添加配置

    dependencies {
    	...
        implementation project(':aurora-imui-react-native')
    
  5. android\app\src\main\AndroidManifest.xml

    <manifest 
     xmlns:tools="http://schemas.android.com/tools" // 这里是新添加的
    >
        <application
          android:allowBackup="false"
          tools:replace="android:allowBackup" // 这里是新添加的
          >
    
  6. 实例demo

react-native-scrollable-tab-view

顶部tab栏

使用

  1. 安装依赖

    yarn add   react-native-scrollable-tab-view @react-native-community/viewpager
    
  2. 使用

    标签最外层必须为 ScrollableTabView

      
    import React from 'react';
    import {
      Text,
    } from 'react-native';
    
    import ScrollableTabView, { DefaultTabBar } from 'react-native-scrollable-tab-view';
    
    export default () => {
      return <ScrollableTabView
        style={{ marginTop: 20 }}
        initialPage={1}
        renderTabBar={() => <DefaultTabBar />}
      >
        <Text tabLabel='Tab #1'>My</Text>
        <Text tabLabel='Tab #2'>favorite</Text>
        <Text tabLabel='Tab #3'>project</Text>
      </ScrollableTabView>;
    }
    

moment

流行的日期库

  1. 下载

    yarn add moment   
    
  2. 设置语言

    import moment from "moment";
    import 'moment/locale/zh-cn';
    moment.locale('zh-cn');
    
    export default moment;
    

    react-native-image-picker 图片选择工具

  3. 安装

    yarn add react-native-image-picker
    

mobx mobx-react

  1. 安装mobx mobx-react
yarn add --save-dev mobx mobx-react
  1. 因为要用到装饰器,所以需要安装一个依赖去解析
yarn add @babel/plugin-proposal-decorators
  1. 然后在.babelrc配置
['@babel/plugin-proposal-decorators', { 'legacy': true }]
  1. 使用
import {Provider} from 'mobx-react'
import RootStore from './src/mobx'

<Provider RootStore ={RootStore} UserStore={UserStore}>
     {this.state.isInit ? <Routers></Routers> : <></>}
</Provider>

三、错误记录

1、‘react-native’ 不是内部或外部命令,也不是可运行的程序 或批处理文件。

(1)解决方法 在vscode 不能使用 但是在Windows cmd 中可以使用命令
(2)解决
1.查看npm目录下是否安装react-native-cli
2. 安装react-native-cli:

npm install -g react-native-cli
2、VSCode中"experimentalDecorators"设置

解决:搜索tsconfig.json勾选保存 在这里插入图片描述

3、开发中调试 react-native-debugger
4、debugger中不能查看请求

解决 在 index.js中加入

import 'react-native-gesture-handler';
GLOBAL.XMLHttpRequest = GLOBAL.originalXMLHttpRequest || GLOBAL.XMLHttpRequest;
5、Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081

然后把Android项目中local.properties文件拷贝到RN项目Android文件夹中
local.properties内容是sdk目录

sdk.dir = C:/Users/Mars/AppData/Local/Android/sdk
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值