RN:macOS上安装RN

一、安装RN

    1. 安装homebrew
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
    
    homebrew在安装软件时可能会碰到 /usr/local 目录不可写的权限问题。可以使用下面的命令修复:
    sudo chown -R `whoami` /usr/local
    
    1. 安装node
    brew install node
    
    1. 切换源
    npm config set registry https://registry.npm.taobao.org --global
    npm config set disturl https://npm.taobao.org/dist --global
    
    1. 安装yarn(Yarn是 Facebook 提供的替代 npm 的工具,可以加速 node 模块的下载。)
    npm install -g yarn react-native-cli
    
    1. 设置yarn的源
    yarn config set registry https://registry.npm.taobao.org --global
    yarn config set disturl https://npm.taobao.org/dist --global
    
    1. 安装watchman
    brew install watchman
    
    以上基本上就把RN给搞好了。

  • a. 创建工程
    react-native init ProjectName
    
  • b. 进入rn根文件路径,启动模拟机运行
    react-native run-ios
    
    或设置默认模拟机机型
    react-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"
    

二、调试

  1. 安装RN Debugger:brew install react-native-debugger
  2. 安装 react-native-debugger-open
  3. start 改为 REACT_DEBUGGER='rndebugger-open --open --port 8081' react-native start

或直接在 index.js 里增加:

import { NativeModules } from 'react-native'

if (__DEV__) {
  NativeModules.DevSettings.setIsDebuggingRemotely(true)
}

参考

  1. RN官网
  2. react-native-debugger
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

相关阅读更多精彩内容

友情链接更多精彩内容