
ReactNative - 开发笔记
关于React-Native
的一些记录和总结。
插件
- 路由导航:react-navigation
- UI组件:react-native-elements
- 网络请求:axios
- 状态管理:redux、redux-thunk、react-redux
- Icon图标:react-native-vector-icons
- 轮播图:react-native-swiper
- 支付宝:react-native-alipay
- 百度地图:react-native-baidu-map
- 弹出输入框:react-native-dialog-input
- HTML渲染:react-native-htmlview
- 欢迎页:react-native-app-intro-slider
- 日期时间选择:datetimepicker
- Fab按钮:react-native-action-button
- 组件相对路径:babel-plugin-root-import
踩坑记录
本地请求失败
在请求本地服务器http://127.0.0.1:7001
的时候怎么也访问不到。后来才了解到原来使用Android Studio
虚拟机,需要访问http://10.0.2.2:28080,相当于开了两台局域网主机嘛。
Axios doesn’t work with Android (emulator) raising a Network Error
导航标题不居中
react-navigation
标题在安卓上默认显示在左边,想在左边做一个回退按钮,按照官方文档设置了标题居中,并没有起效果。解决方案如下:
|
打包无法联网
安卓9开始需要添加配置才能开启联网权限。解决方案如下:
在
/src/main/res
创建network_security_config.xml
文件,内写代码:<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>在
/src/main/androidManifest.xml
加入如下代码:<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:networkSecurityConfig="@xml/network_security_config" //Add
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme">
支付宝开启沙箱
开发支付业务时用到的第三方封装支付宝插件,但是没有集成沙箱环境,按以下方式集成。
android/src/main/java/com/reactlibrary/AlipayModule.java
中添加以下代码:package com.reactlibrary;
import com.alipay.sdk.app.EnvUtils; //Add
import com.alipay.sdk.app.H5PayCallback;
import com.alipay.sdk.util.H5PayResultModel;
import com.alipay.sdk.app.AuthTask;
public void run() {
thread.start();
}
// Add
@ReactMethod
public void setAlipaySandbox(Boolean isSandbox) {
if(isSandbox){
EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
}else {
EnvUtils.setEnv(EnvUtils.EnvEnum.ONLINE);
}
}
@ReactMethod
public void pay(final String orderInfo, final Promise promise) {
Runnable runnable = new Runnable() {index.d.ts
中添加以下代码:function authWithInfo(infoStr: string): IResult;
// Add
function setAlipaySandbox(isSandbox: boolean): void
function pay(infoStr: string): IResult;
function payInterceptorWithUrl(infoStr: string): {
组件导入相对路径配置
安装并配置插件babel-plugin-root-import
即可。
|
获取sha1
在使用 react-native-baidu-map 过程中,发现 reverseGeoCode
等一些功能无法使用,且在RN控制台没有报错。
后来在 Android Studio
打包时发现原生模块报错,原来是 百度地图SKD
中的 开发版sha1 和 发布版sha1 没有配置正确。
百度地图bug
获取方法如下:
|
可以看到 debug.keystore
和 release.keystore
签名文件。(具体文件名可能不同)
|
开发版sha1
提示 输入秘钥库密码
,直接回车,即可获取 开发版sha1
。
发布版sha1
同理:
|
- 本文作者:zhaoo
- 本文链接:https://www.izhaoo.com/2020/01/07/rn-note/index.html
- 版权声明:本博客所有文章均采用 BY-NC-SA 许可协议,转载请注明出处!
