rn_test

说明

简介

安装

全局脚手架

npm i -g react-native-cli

安装基础项目

react-native init <项目名称>

运行

优化后的运行

原始启动

资源

未探索的资源

365个库中寻找77个

  1. select选择控件
  2. 开关式选择列表
  3. 表情选择器
  4. 左右滑动层
  5. 手机号选择控件
  6. 登录动画
  7. 图片查看器
  8. 菜单操作动画
  9. 滑动操作图片展示样式
  10. 操作打字效果
  11. 声音可视化
  12. 占位符
  13. 图片查看器
  14. 模糊背景效果
  15. 本地图片超级操作
  16. 四面八方拖动效果
  17. 信用卡输入效果
  18. 展开动画效果
  19. chart
  20. 初始化启动页
  21. 日期
  22. 左右滑动操作卡片效果
  23. 列表拖拽
  24. chart
  25. 密码锁效果
  26. 星级评价
  27. 手动签字
  28. 滑动时间轴效果
  29. 国家26字母选择
  30. PDF查看
  31. 26字母选择
  32. 分享
  33. 对话框
  34. 电话、地址等操作
  35. 后台信息保护
  36. 图片查看器
  37. 左右滑动动画
  38. 无网络操作
  39. 颜色选择器
  40. 左右滑动tab动效
  41. 加载中效果
  42. 左右滑动效果,类似酷划锁屏
  43. 二维码生成器
  44. 图片查看器
  45. 视差效果
  46. 日期
  47. 点击效果
  48. 扇形菜单
  49. 手势识别

新增页面需要在三个地方进行操作

  1. data里面的complete.js中添加数据,方便在完成的探索中出现
  2. navigators中的AppStackNavigators.js里面进行引入=>注册,使得路由正常
  3. pages里面添加页面

优化package.json使开发更加方便

"ios":"react-native run-ios",
"android":"react-native run-android",
"s":"npm run ios && npm run android",

flex布局常用属性

display: 'flex';
flexDirection: row | row-reverse | column | column-reverse;
flexWrap: nowrap | wrap | wrap-reverse;
flexFlow: <flex-direction> || <flex-wrap>;
justifyContent: flex-start | flex-end | center | space-between | space-around;
alignItems: flex-start | flex-end | center | baseline | stretch;
alignContent: flex-start | flex-end | center | space-between | space-around | stretch;

order: <integer>;
flexGrow: <number>; /* default 0 */
flexShrink: <number>; /* default 1 */
flexBasis: <length> | auto; /* default auto */
flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
alignSelf: auto | flex-start | flex-end | center | baseline | stretch;

图片

图片使用

<Image source={require('./my-icon.png')} />

命名规则

链接原生库

第一步

安装带原生依赖的库

npm install <某个带有原生依赖的库> --save

第二步

运行以下命令,它会根据package.json文件中的dependenciesdevDependencies记录来链接所有需要链接的库

react-native link

开发技巧

处理触摸事件

使用"Touchable"开头的一系列组件
通过onPress属性接受一个点击事件的处理函数

_onPressButton=((item)=>{
  alert(1)
  console.log(item);
})
render() {
  return (
    <View>
      <TouchableHighlight onPress={this._onPressButton}>
        <View style={[S.btn,{backgroundColor:`${this.state.bg}`}]}>
          <Text style={S.btnText}>{this.state.bg}</Text>
        </View>
      </TouchableHighlight>
      <TouchableHighlight onPress={()=>{
        this._onPressButton(this.state.bg)
      }}>
        <View style={[S.btn,{backgroundColor:`${this.state.bg}`}]}>
          <Text style={S.btnText}>{this.state.bg}</Text>
        </View>
      </TouchableHighlight>
    </View>
  );
}

解决版本冲突问题

js版本和rn版本出现冲突

解决路由失效问题

this.props.navigation属性详细说明

AsyncStorage保存报错

JSON value '<null>' of type NSNull

react-native支持的style props

Valid style props: [
  "alignContent",
  "alignItems",
  "alignSelf",
  "aspectRatio",
  "backfaceVisibility",
  "backgroundColor",
  "borderBottomColor",
  "borderBottomEndRadius",
  "borderBottomLeftRadius",
  "borderBottomRightRadius",
  "borderBottomStartRadius",
  "borderBottomWidth",
  "borderColor",
  "borderEndColor",
  "borderEndWidth",
  "borderLeftColor",
  "borderLeftWidth",
  "borderRadius",
  "borderRightColor",
  "borderRightWidth",
  "borderStartColor",
  "borderStartWidth",
  "borderStyle",
  "borderTopColor",
  "borderTopEndRadius",
  "borderTopLeftRadius",
  "borderTopRightRadius",
  "borderTopStartRadius",
  "borderTopWidth",
  "borderWidth",
  "bottom",
  "color",
  "decomposedMatrix",
  "direction",
  "display",
  "elevation",
  "end",
  "flex",
  "flexBasis",
  "flexDirection",
  "flexGrow",
  "flexShrink",
  "flexWrap",
  "fontFamily",
  "fontSize",
  "fontStyle",
  "fontVariant",
  "fontWeight",
  "height",
  "includeFontPadding",
  "justifyContent",
  "left",
  "letterSpacing",
  "lineHeight",
  "margin",
  "marginBottom",
  "marginEnd",
  "marginHorizontal",
  "marginLeft",
  "marginRight",
  "marginStart",
  "marginTop",
  "marginVertical",
  "maxHeight",
  "maxWidth",
  "minHeight",
  "minWidth",
  "opacity",
  "overflow",
  "overlayColor",
  "padding",
  "paddingBottom",
  "paddingEnd",
  "paddingHorizontal",
  "paddingLeft",
  "paddingRight",
  "paddingStart",
  "paddingTop",
  "paddingVertical",
  "position",
  "resizeMode",
  "right",
  "rotation",
  "scaleX",
  "scaleY",
  "shadowColor",
  "shadowOffset",
  "shadowOpacity",
  "shadowRadius",
  "start",
  "textAlign",
  "textAlignVertical",
  "textDecorationColor",
  "textDecorationLine",
  "textDecorationStyle",
  "textShadowColor",
  "textShadowOffset",
  "textShadowRadius",
  "textTransform",
  "tintColor",
  "top",
  "transform",
  "transformMatrix",
  "translateX",
  "translateY",
  "width",
  "writingDirection",
  "zIndex"
]

StatusBar解决android状态栏问题

设置StatusBar

<StatusBar
     backgroundColor="transparent"
     translucent
   />

设置顶部模块的样式

height: 50,
paddingTop: 10,

Platform区分ios and android平台

三种方法实现跨平台:

  1. Platform.OS
  2. 使用Platformselect方法
  3. 使用不能组件名:ios使用header.ios.js;android使用header.android.js
const instructions = Platform.select({
  ios: 'ios',
  android: 'android',
});

请求fetch封装

引入

import HttpUtils from '../../HttpUtils';

使用

HttpUtils.post(`${apiConfig}`, {
        platform: 'ios'
    },global.data.token)
    .then(rtn => {
        global.data.config = rtn;
        // console.log(`配置=>( ${JSON.stringify(rtn)} )`);
    })
    .catch(err => {
        console.log('error', err)
    })

请求环境+请求地址定义

请求环境

请求地址

使用

import addrList from '../../addr';
const {apiConfig}=addrList;

react-native-scrollable-tab-view

react-native初始化报错,导致无法初始化

npm config set registry https://registry.npm.taobao.org
npm config set disturl https://npm.taobao.org/dist

动态切换状态

声明周期-初始渲染 and 检测goBack()

componentDidMount(){}

检测goBack()

需要重新加载app

第一步

componentWillUnmount(){
  DeviceEventEmitter.emit('goBack', 'goBack刷新返回页面数据');
}

第二步

componentDidMount(){
  // 接收
  this.deEmitter = DeviceEventEmitter.addListener('goBack', (a) => {
    this.loadData();
  });
}

安卓打包

第一步:生成Android签名证书

第二步:设置gradle变量

MYAPP_RELEASE_STORE_FILE=ch09-keystore.jks
MYAPP_RELEASE_KEY_ALIAS=ch09-keystore
MYAPP_RELEASE_STORE_PASSWORD=123456
MYAPP_RELEASE_KEY_PASSWORD=123456

第三步:在gradle配置文件中添加签名配置

...
android {
    ...
    defaultConfig { ... }
    signingConfigs {
        release {
            storeFile file(MYAPP_RELEASE_STORE_FILE)
            storePassword MYAPP_RELEASE_STORE_PASSWORD
            keyAlias MYAPP_RELEASE_KEY_ALIAS
            keyPassword MYAPP_RELEASE_KEY_PASSWORD
        }
    }
    buildTypes {
        release {
            ...
            signingConfig signingConfigs.release
        }
    }
}
...

第四步:签名打包APK

./gradlew assembleRelease
/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = true

错误处理

fetch(apiLogin, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',//通过头指定,获取的数据类型是JSON
      'Content-Type': 'application/json',
      'Authorization':global.data.token,
    },
    body: JSON.stringify({
      // 请求参数
    })
  })
  .then(result => {
    const {status,statusText} = result;
    if(status>=200&&status<300||status==304){
      // 成功
      const promise = result.json()
      promise.then(rtn=>{
        // 成功
      })
    }else if(status==403){
      // 跳转登录
      this.props.navigation.replace('login');
    }else{
      // 提醒 statusText
      this.refs.toast.show(statusText);
    }
  })
  .catch(error => {
    if(Object.prototype.toString.call(error) === '[object Object]'){
        this.refs.toast.show(JSON.stringify(error))
    }else{
        this.refs.toast.show(error)
    }
  })

app的logo and name

android

LOGO

app名称

IOS

LOGO

app名称