1.环境搭建
安装依赖
必须安装的依赖有:Node、Watchman 和 React Native 命令行工具以及 Xcode,推荐使用Homebrew来安装 Node 和 Watchman,watchman则是由 Facebook 提供的监视文件系统变更的工具。安装此工具可以提高开发时的性能(packager 可以快速捕捉文件的变化从而实现实时刷新)
Homebrew的安装,输入一下命令行:
/usr/bin/ruby -e "$(curl -fshttps://raw.githubusercontent.com/Homebrew/install/master/install)"
Node 和 Watchman的安装,在命令行中执行下列命令安装:
brew install nodebrew brew install watchman
安装完 Node 后建议设置 npm 需要设置如下镜像:
npm config set registry https://registry.npm.taobao.org --global npm config set disturl https://npm.taobao.org/dist --global
Yarn、React Native 的命令行工具(react-native-cli):
输入一下命令行:
npm install -g yarn react-native-cli
安装完 yarn 后同理也要设置镜像源:
yarn config set registry https://registry.npm.taobao.org --global yarn config set disturl https://npm.taobao.org/dist --global
Yarn可以替代 npm 的工具,可以加速 node 模块的下载。React Native 的命令行工具用于执行创建、初始化、更新项目、运行打包服务(packager)等任务。
Xcode安装
可以通过 App Store 或是到上apple开发者官网下载
2.创建新项目
注意!!!:init 命令默认会创建最新的版本,新版本可能会存在运行报错的问题。而目前最新的 0.45 及以上版本需要下载 boost 等几个第三方库编译。这些库在国内即便翻墙也很难下载成功,导致很多人无法运行iOS项目!!!中文网在论坛中提供了这些库的国内下载链接。如果你嫌麻烦,又没有对新版本的需求,那么可以暂时创建0.44.3的版本。
创建新项目输入命令行:
react-native init myApp
具体查看https://reactnative.cn/docs/getting-started.html
网友评论