react-native-dev
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReact Native & Expo Development Guide
React Native & Expo 开发指南
A practical guide for building production-ready React Native and Expo applications. Covers UI, animations, state, testing, performance, and deployment.
一份用于构建生产级React Native与Expo应用的实用指南,涵盖UI、动画、状态、测试、性能及部署等内容。
References
参考资料
Consult these resources as needed:
- references/navigation.md — Expo Router: Stack, Tabs, NativeTabs (,
headerLargeTitle), links, modals, sheets, context menusheaderBackButtonDisplayMode - references/components.md — FlashList patterns, , safe areas (
expo-image), native controls, blur/glass effects, storagecontentInsetAdjustmentBehavior - references/styling.md — StyleSheet, NativeWind/Tailwind, platform styles, theming, dark mode
- references/animations.md — Reanimated 3: entering/exiting, shared values, gestures, scroll-driven
- references/state-management.md — Zustand (selectors, persist), Jotai (atoms, derived), React Query, Context
- references/forms.md — React Hook Form + Zod: validation, multi-step, dynamic arrays
- references/networking.md — fetch wrapper, React Query (optimistic updates), auth tokens, offline, API routes, webhooks
- references/performance.md — Profiling workflow, FlashList + , bundle analysis, TTI, memory leaks, animation perf
memo - references/testing.md — Jest, React Native Testing Library, E2E with Maestro
- references/native-capabilities.md — Camera, location, permissions (hooks), haptics, notifications, biometrics
use*Permissions - references/engineering.md — Project layout (,
components/ui/,stores/), path aliases, SDK upgrades, EAS build/submit, CI/CD, DOM componentsservices/
按需查阅以下资源:
- references/navigation.md — Expo Router:Stack、Tabs、NativeTabs(、
headerLargeTitle)、链接、模态框、底部弹窗、上下文菜单headerBackButtonDisplayMode - references/components.md — FlashList模式、、安全区域(
expo-image)、原生控件、模糊/毛玻璃效果、存储contentInsetAdjustmentBehavior - references/styling.md — StyleSheet、NativeWind/Tailwind、平台样式、主题、暗黑模式
- references/animations.md — Reanimated 3:入场/退场动画、共享值、手势、滚动驱动动画
- references/state-management.md — Zustand(选择器、持久化)、Jotai(原子、派生状态)、React Query、Context
- references/forms.md — React Hook Form + Zod:验证、多步骤表单、动态数组
- references/networking.md — fetch封装、React Query(乐观更新)、认证令牌、离线处理、API路由、Webhooks
- references/performance.md —性能分析流程、FlashList + 、包分析、TTI、内存泄漏、动画性能
memo - references/testing.md — Jest、React Native Testing Library、Maestro端到端测试
- references/native-capabilities.md — 相机、定位、权限(钩子)、触觉反馈、通知、生物识别
use*Permissions - references/engineering.md — 项目布局(、
components/ui/、stores/)、路径别名、SDK升级、EAS构建/提交、CI/CD、DOM组件services/
Quick Reference
快速参考
Component Preferences
组件偏好
| Purpose | Use | Instead of |
|---|---|---|
| Lists | | |
| Images | | RN |
| Press | | |
| Audio | | |
| Video | | |
| Animations | Reanimated 3 | RN Animated API (limited) |
| Gestures | Gesture Handler | PanResponder (legacy) |
| Platform check | | |
| Context | | |
| Safe area scroll | | |
| SF Symbols | | |
| 用途 | 推荐使用 | 替代方案 |
|---|---|---|
| 列表 | | |
| 图片 | | RN |
| 点击交互 | | |
| 音频 | | |
| 视频 | | |
| 动画 | Reanimated 3 | RN Animated API(功能有限) |
| 手势 | Gesture Handler | PanResponder(旧版) |
| 平台检测 | | |
| Context | | |
| 安全区域滚动 | | |
| SF Symbols | 使用 | |
Scaling Up
规模扩展建议
| Situation | Consider |
|---|---|
| Long lists with scroll jank | Virtualized list libraries (e.g. FlashList) |
| Want Tailwind-style classes | NativeWind v4 |
| High-frequency storage reads | Sync-based storage (e.g. MMKV) |
| New project with Expo | Expo Router over bare React Navigation |
| 场景 | 考虑方案 |
|---|---|
| 长列表滚动卡顿 | 虚拟化列表库(如FlashList) |
| 想要Tailwind风格的类名 | NativeWind v4 |
| 高频存储读取 | 同步存储(如MMKV) |
| 新Expo项目 | 使用Expo Router而非原生React Navigation |
State Management
状态管理
| State Type | Solution |
|---|---|
| Local UI state | |
| Shared app state | Zustand or Jotai |
| Server / async data | React Query |
| Form state | React Hook Form + Zod |
| 状态类型 | 解决方案 |
|---|---|
| 本地UI状态 | |
| 全局共享状态 | Zustand或Jotai |
| 服务端/异步数据 | React Query |
| 表单状态 | React Hook Form + Zod |
Performance Priorities
性能优化优先级
| Priority | Issue | Fix |
|---|---|---|
| CRITICAL | Long list jank | |
| CRITICAL | Large bundle | Avoid barrel imports, enable R8 |
| HIGH | Too many re-renders | Zustand selectors, React Compiler |
| HIGH | Slow startup | Disable bundle compression, native nav |
| MEDIUM | Animation drops | Only animate |
| 优先级 | 问题 | 修复方案 |
|---|---|---|
| 关键 | 长列表滚动卡顿 | |
| 关键 | 包体积过大 | 避免桶导入、启用R8 |
| 高 | 重渲染过多 | Zustand选择器、React Compiler |
| 高 | 启动缓慢 | 禁用包压缩、使用原生导航 |
| 中 | 动画掉帧 | 仅对 |
New Project Init
新项目初始化
bash
undefinedbash
undefined1. Create project
1. Create project
npx create-expo-app@latest my-app --template blank-typescript
cd my-app
npx create-expo-app@latest my-app --template blank-typescript
cd my-app
2. Install Expo Router + core deps
2. Install Expo Router + core deps
npx expo install expo-router react-native-safe-area-context react-native-screens
npx expo install expo-router react-native-safe-area-context react-native-screens
3. (Optional) Common extras
3. (Optional) Common extras
npx expo install expo-image react-native-reanimated react-native-gesture-handler
Then configure:
1. Set entry point in `package.json`: `"main": "expo-router/entry"`
2. Add scheme in `app.json`: `"scheme": "my-app"`
3. Delete `App.tsx` and `index.ts`
4. Create `app/_layout.tsx` as root Stack layout
5. Create `app/(tabs)/_layout.tsx` for tab navigation
6. Create route files in `app/(tabs)/` (see [navigation.md](references/navigation.md))
For web support, also install: `npx expo install react-native-web react-dom @expo/metro-runtime`npx expo install expo-image react-native-reanimated react-native-gesture-handler
然后进行配置:
1. 在`package.json`中设置入口文件:`"main": "expo-router/entry"`
2. 在`app.json`中添加scheme:`"scheme": "my-app"`
3. 删除`App.tsx`和`index.ts`
4. 创建`app/_layout.tsx`作为根Stack布局
5. 创建`app/(tabs)/_layout.tsx`用于标签页导航
6. 在`app/(tabs)/`目录下创建路由文件(详见[navigation.md](references/navigation.md))
如需支持Web平台,还需安装:`npx expo install react-native-web react-dom @expo/metro-runtime`Core Principles
核心原则
Consult references before writing: when implementing navigation, lists, networking, or project setup, read the matching reference file above for patterns and pitfalls.
Try Expo Go first (). Custom builds () only needed when using local Expo modules, Apple targets, or third-party native modules not in Expo Go.
npx expo starteas buildConditional rendering: use not (renders "0").
{count > 0 && <Text />}{count && <Text />}Animation rule: only animate and — GPU-composited, no layout thrash.
transformopacityImports: always import directly from source, not barrel files — avoids bundle bloat.
Lists and images: before using or RN , check the Component Preferences table above — and are almost always the right choice.
FlatListImageFlashListexpo-imageRoute files: always use kebab-case, never co-locate components/types/utils in .
app/编写代码前先查阅参考资料:在实现导航、列表、网络或项目搭建时,先阅读上述对应的参考文档,了解最佳实践和常见陷阱。
优先使用Expo Go()。仅当使用本地Expo模块、Apple目标平台或Expo Go不支持的第三方原生模块时,才需要自定义构建()。
npx expo starteas build条件渲染:使用而非(后者会渲染"0")。
{count > 0 && <Text />}{count && <Text />}动画规则:仅对和属性做动画——这些属性由GPU合成,不会导致布局抖动。
transformopacity导入规则:始终直接从源文件导入,而非从桶文件导入——避免包体积膨胀。
列表与图片:在使用或RN 前,先查看上方的组件偏好表——和几乎总是更好的选择。
FlatListImageFlashListexpo-image路由文件:始终使用短横线命名法(kebab-case),不要在目录下存放组件/类型/工具类文件。
app/Checklist
检查清单
New Project Setup
新项目搭建
- path aliases configured
tsconfig.json - env var set per environment
EXPO_PUBLIC_API_URL - Root layout has (if using gestures)
GestureHandlerRootView - on all scroll views
contentInsetAdjustmentBehavior="automatic" - instead of
FlashListfor lists > 20 itemsFlatList
- 配置中的路径别名
tsconfig.json - 为不同环境设置环境变量
EXPO_PUBLIC_API_URL - 根布局中包含(如果使用手势)
GestureHandlerRootView - 所有滚动视图设置
contentInsetAdjustmentBehavior="automatic" - 对于超过20项的列表,使用替代
FlashListFlatList
Before Shipping
发布前检查
- Profile in mode, fix frames > 16ms
--profile - Bundle analyzed (), no barrel imports
source-map-explorer - R8 enabled for Android
- Unit + component tests for critical paths
- E2E flows for login, core feature, checkout
Flutter development → see skill.
iOS native (UIKit/SwiftUI) → see skill.
Android native (Kotlin/Compose) → see skill.
flutter-devios-application-devandroid-native-devReact Native is a trademark of Meta Platforms, Inc. Expo is a trademark of 650 Industries, Inc. All other product names are trademarks of their respective owners.
- 使用模式进行性能分析,修复帧耗时超过16ms的问题
--profile - 分析包体积(),移除桶导入
source-map-explorer - 为Android启用R8
- 为关键路径编写单元测试和组件测试
- 为登录、核心功能、结账流程编写端到端测试
Flutter开发 → 查看技能文档。
iOS原生(UIKit/SwiftUI)→ 查看技能文档。
Android原生(Kotlin/Compose)→ 查看技能文档。
flutter-devios-application-devandroid-native-devReact Native是Meta Platforms, Inc.的商标。Expo是650 Industries, Inc.的商标。所有其他产品名称均为其各自所有者的商标。