githu源码传送门java
本文主要包含如下几部分:react
关注公众号
剪刀手解柒 发送
RN初始化
获取完整视频 。
React Native 中文网之搭建开发环境android
项目初始化能够参考官方文档,写的比较详细。其中,新建项目主要有如下步骤:ios
npx react-native init RNLaboratory
cd RNLaboratory react-native run-ios
因为 RN 0.60+ 版本依赖 CocoaPods,可是国内难以访问此仓库。因此,在执行 npx react-native init RNLaboratory
进行初始化项目时会卡在 info Installing required CocoaPods dependencies,以下图所示:git
此时能够安装国内镜像,参考文档。若实现安装好此镜像,当执行 npx react-native init RNLaboratory
时,会发现初始化项目很快,如图所示:github
当以上步骤顺利完成后,使用命令 react-native run-ios
启动 APP
,以下所示:npm
在使用React Native开发移动应用程序时,最重要的步骤之一就是选择合适的导航库。通常来讲,有如下两种选择方案:vim
在项目 React Native 实验室 中选择了 react-navigation
做为导航库,在此不作详细对比,有兴趣的同窗能够参考如下连接:react-native
可参考官方文档,其主要流程以下:xcode
react-navigation
yarn add react-navigation # or with npm npm install react-navigation
react-navigation
相关依赖yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context
cd ios pod install cd ..
android/app/build.gradle
, 在该文件 dependencies
中添加以下两行代码:implementation 'androidx.appcompat:appcompat:1.1.0-rc01' implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0-alpha02'
android/app/src/main/java/com/APP_NAME/MainActicity.java
,以下所示:package com.reactnavigation.example; import com.facebook.react.ReactActivity; + import com.facebook.react.ReactActivityDelegate; + import com.facebook.react.ReactRootView; + import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView; public class MainActivity extends ReactActivity { @Override protected String getMainComponentName() { return "Example"; } + @Override + protected ReactActivityDelegate createReactActivityDelegate() { + return new ReactActivityDelegate(this, getMainComponentName()) { + @Override + protected ReactRootView createRootView() { + return new RNGestureHandlerEnabledRootView(MainActivity.this); + } + }; + } }
react-navigation 提供了简单易用的跨平台导航方案,在 iOS 和 Android 上均可以进行翻页式、tab 选项卡式和抽屉式的导航布局。其提供如下7个API用于建立不一样的导航方式。(此处不作过多介绍,毕竟 react-navigation 官方网站 写的不错...😁😁😁)
注意:
react-navigation
移至相应的库,需安装相应的依赖。react-native-vector-icons
时出现如下错误,可参考 How to use vector icons in your react-native project 解决。在项目 React Native 实验室 中基于 createStackNavigator
、createSwitchNavigator
、createDrawerNavigator
及 createMaterialBottomTabNavigator
等实现了基础路由跳转功能。效果图以下:
欢迎关注个人我的公众号:剪刀手解柒