一、安装RN
-
- 安装homebrew
homebrew在安装软件时可能会碰到/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"/usr/local目录不可写的权限问题。可以使用下面的命令修复:sudo chown -R `whoami` /usr/local -
- 安装node
brew install node -
- 切换源
npm config set registry https://registry.npm.taobao.org --global npm config set disturl https://npm.taobao.org/dist --global -
- 安装yarn(Yarn是 Facebook 提供的替代 npm 的工具,可以加速 node 模块的下载。)
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 -
- 安装watchman
以上基本上就把RN给搞好了。brew install watchman
- a. 创建工程
react-native init ProjectName - b. 进入rn根文件路径,启动模拟机运行
或设置默认模拟机机型react-native run-iosreact-native run-ios --simulator "iPhone 8" - c. 模拟机启动失败,配置模拟机
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer/
Apple M1安装ruby:
之后将以下三个命令写入brew install ruby~/.zshrc:export PATH="/usr/local/opt/ruby/bin:$PATH" export LDFLAGS="-L/usr/local/opt/ruby/lib" export CPPFLAGS="-I/usr/local/opt/ruby/include"
二、调试
- 安装RN Debugger:
brew install react-native-debugger; - 安装
react-native-debugger-open; - start 改为
REACT_DEBUGGER='rndebugger-open --open --port 8081' react-native start;
或直接在 index.js 里增加:
import { NativeModules } from 'react-native'
if (__DEV__) {
NativeModules.DevSettings.setIsDebuggingRemotely(true)
}
参考