当前位置: 首页 > news >正文

ReactNative开发实战——React Native开发环境配置指南

一、开发前准备

1. macOS平台基础工具安装

brew install node@18
brew install watchman
brew install cocoapods

2. 代理配置

npm config set proxy http://127.0.0.1:7890
npm config set https-proxy http://127.0.0.1:7890# 新增扩展建议(可选配置)
echo 'export ALL_PROXY=http://127.0.0.1:7890' >> ~/.zshrc  # 全局代理设置
git config --global http.proxy http://127.0.0.1:7890       # Git代理设置

二、项目创建

1. 初始化项目

npx @react-native-community/cli init AwesomeProject

2. pnpm支持配置

// metro.config.js
const path = require('path');
const { getDefaultConfig } = require('metro-config');module.exports = (async () => {const {resolver: { sourceExts, assetExts },} = await getDefaultConfig();return {resolver: {extraNodeModules: new Proxy({},{get: (_, name) => path.join(process.cwd(), `node_modules/${name}`),}),assetExts: [...assetExts, 'hcscript'],sourceExts: [...sourceExts, 'ts', 'tsx'],},transformer: {getTransformOptions: async () => ({transform: {experimentalImportSupport: false,inlineRequires: true,},}),},};
})();

三、项目结构配置

1. 目录结构创建

mkdir -p src/{assets/{fonts,images},components/{common,glucose,charts},constants,contexts,hooks,navigation,screens/{TodayScreen,TrendsScreen,ProfileScreen},services,store,styles,utils}

结构示意图

src/
├── assets/          # 静态资源
│   ├── fonts/       # 字体文件
│   └── images/      # 图片资源
├── components/      # 组件库
│   ├── common/      # 通用组件
│   ├── glucose/     # 血糖相关组件(保留您特定业务组件)
│   └── charts/      # 图表组件
└── ...              # 其他原始目录结构

四、开发配置优化

1. TypeScript路径别名

// tsconfig.json
{"compilerOptions": {"baseUrl": ".","paths": {"@assets/*": ["src/assets/*"],"@components/*": ["src/components/*"]// ...其他原始别名配置}}
}

2. Babel配置增强

// babel.config.js
module.exports = {presets: ['module:@react-native/babel-preset'],plugins: [['module-resolver',{root: ['./src'],extensions: ['.ios.js','.android.js', '.js','.ts','.tsx','.json'],alias: {'@assets': './src/assets','@components': './src/components',// ...其他别名}}],'react-native-worklets/plugin' ]
};

五、编译加速方案

1. 多层级代理配置(新增)

# Android专属代理(gradle.properties)
echo "systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=7890
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=7890" > ~/.gradle/gradle.properties# CocoaPods代理
echo "install! 'cocoapods', :http_proxy => 'http://127.0.0.1:7890', :https_proxy => 'http://127.0.0.1:7890'" >> Podfile

2. 镜像源加速(兼容性扩展)

# 可选清华源
npm config set registry https://registry.npmmirror.com
pod repo add tsinghua https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git

六、验证方案

1. 环境检查脚本

#!/bin/zsh
echo "=== 环境验证报告 ==="
echo "Node版本: $(node -v)"
echo "npm镜像源: $(npm config get registry)"
echo "PNPM版本: $(pnpm -v 2>/dev/null || echo '未安装')"
echo "iOS工具链: $(pod --version 2>/dev/null || echo '未配置')"
echo "Android构建: $(grep maven.aliyun.com android/build.gradle 2>/dev/null && echo '阿里云镜像已配置' || echo '未检测到镜像')"
http://www.lryc.cn/news/624902.html

相关文章:

  • 机器翻译论文阅读方法:顶会(ACL、EMNLP)论文解析技巧
  • ADC的实现(单通道,多通道,DMA)
  • 如何编写自己的Spring容器
  • 【EI会议征稿】2025第四届健康大数据与智能医疗国际会议(ICHIH 2025)
  • VS Code Copilot 完整使用教程(含图解)
  • 全局锁应用场景理解
  • 深度学习——R-CNN及其变体
  • 04 类型别名type + 检测数据类型(typeof+instanceof) + 空安全+剩余和展开(运算符 ...)简单类型和复杂类型 + 模块化
  • Spark 运行流程核心组件(三)任务执行
  • 实习两个月总结
  • [系统架构设计师]软件架构的演化与维护(十)
  • SpringBoot--JWT
  • 大数据计算引擎(四)—— Impala
  • React diff——差异协调算法简介
  • 深入解析 Qwen3 GSPO:一种稳定高效的大语言模型强化学习算法
  • 整体设计 之“凝聚式中心点”原型 --整除:智能合约和DBMS的深层融合 之2
  • LLM - MCP传输协议解读:从SSE的单向奔赴到Streamable HTTP的双向融合
  • 【软考架构】第4章 信息安全的抗攻击技术
  • 群晖nas中 打开PHP连接MariaDB 功能扩展
  • CMakeLists.txt 学习笔记
  • SQL详细语法教程(六)存储+索引
  • Vue3+Vite MPA多页面应用开发完整指南 – 从零搭建到部署优化
  • 博客项目 Spring + Redis + Mysql
  • Linx--MySQL--安装笔记详细步骤!
  • B4265 [朝阳区小学组 2019] rectangle
  • SpringAI集成MCP
  • CentOS 7更换国内镜像源
  • SQL Server 基本语法
  • 传统方式部署(RuoYi-Cloud)微服务
  • 云原生:重塑软件世界的技术浪潮与编程语言选择