环境搭建
按照官网的中文指引文档即可
搭建开发环境
工程构建
使用react-native init 命令来创建一个RN工程,如下所示
# 工程目录名是驼峰命名规则
react-native ini helloRN
成功创建后有如下提示,运行应用(推荐使用VS code来打工工程)
Run instructions for iOS:
• cd /Users/luogw/temp/helloRN && react-native run-ios
- or -
• Open ios/helloRN.xcodeproj in Xcode
• Hit the Run button
Run instructions for Android:
• Have an Android emulator running (quickest way to get started), or a device connected.
• cd /Users/luogw/temp/helloRN && react-native run-android
基础CLI命令
管理依赖
# 重新安装node_modules
npm install
# 添加依赖 react-native-vector-icons
npm install react-native-vector-icons
# 移除依赖库(如果做了link的依赖,行移除前,先进行unlink,然后才能进行移除操作
npm uninstall react-native-vector-icons
# 链接所有可以自动链接的依赖库
react-native link
# 链接某个依赖 如 react-native-vector-icons
react-native link react-native-vector-icons
# 断开链接单个依赖:react-native-vector-icons
react-native unlink react-native-vector-icons
# 移除 node_modules
rm -rf node_modules
应用打包
react-native命令
luogw@luogw-mbp studyRN$ react-native --help
Usage: react-native [options] [command]
Options:
--version Print CLI version
--projectRoot [string] Path to the root of the project
--reactNativePath [string] Path to React Native
--verbose Increase logging verbosity
-h, --help output usage information
Commands:
start [options] starts the webserver
run-ios [options] builds your app and starts it on iOS simulator
run-android [options] builds your app and starts it on a connected Android emulator or device
new-library [options] generates a native library bridge
bundle [options] builds the javascript bundle for offline use
ram-bundle [options] builds javascript as a "Random Access Module" bundle for offline use
eject [options] Re-create the iOS and Android folders and native code
link [options] [packageName] scope link command to certain platforms (comma-separated)
unlink [options] <packageName> unlink native dependency
install [options] <packageName> install and link native dependencies
uninstall [options] <packageName> uninstall and unlink native dependencies
upgrade [options] [version] Upgrade your app's template files to the specified or latest npm version using `rn-diff-purge` project. Only valid semver versions are allowed.
log-android [options] starts adb logcat
log-ios [options] starts iOS device syslog tail
dependencies [options] lists dependencies
info [options] Get relevant version info about OS, toolchain and libraries
init [options]
调试技巧
引入第三方库
引入第二方库,有些是库是需要引入本地资源的,需要react-native link相关的资源
如引入react-native-elements库时