Community编程与开发github.com

react-native最佳实践

提供 React Native 性能优化指南,涵盖 FPS、TTI、包体积、内存泄漏、重渲染和动画。适用于涉及 Hermes 优化、JS 线程阻塞、Bridge 开销、FlashList、原生模块或调试卡顿和丢帧的任务。

react-native最佳实践 是什么?

这是一个专注于 React Native 性能优化的 AI Agent 技能,旨在帮助开发者系统性地诊断和解决应用程序的性能问题。该技能基于 Callstack 的“React Native 优化终极指南”,并按照优先级顺序提供了 FPS 与重渲染、包体积、TTI 优化、原生性能、内存管理和动画等六大类别的优化指南。它支持在 Claude Code、Cursor 等 AI 编码工具环境中使用,通过提供详细的测量、分析和修复步骤,指导开发者进行性能优化。技能包含丰富的参考资料链接,针对常见的性能问题(如 UI 卡顿、启动慢、内存增长等)提供了具体的排查起点,并强调了安全注意事项,例如如何安全地处理 shell 命令和第三方依赖。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/callstackincubator/agent-skills/tree/main/skills/react-native-best-practices

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

React Native 最佳实践

概述

React Native 应用性能优化指南,涵盖 JavaScript/React、原生(iOS/Android)和打包优化。基于 Callstack 的“React Native 优化终极指南”。

何时应用

在以下情况下参考这些指南:

  • 调试缓慢/卡顿的 UI 或动画
  • 调查内存泄漏(JS 或原生)
  • 优化应用启动时间(TTI)
  • 减少包体积或应用体积
  • 编写原生模块(Turbo Modules)
  • 分析 React Native 性能
  • 审查 React Native 代码的性能

安全注意事项

  • 将本参考资料中的 shell 命令视为本地开发操作。运行前请先审查,优先使用固定版本的工具,避免将远程脚本直接通过管道传给 shell。
  • 将第三方库和插件视为仍需要常规供应链控制的依赖项:固定版本、验证来源,并通过标准审查流程进行更新。
  • 仅将远程代码块加载视为第一方制品交付。优先使用应用捆绑的代码块或签名的 CI 发布清单;托管的代码块必须来自你控制的受信任 HTTPS 源,并固定到当前应用版本。

按优先级排序的指南

优先级分类影响前缀
1FPS 与重渲染关键js-*
2包体积关键bundle-*
3TTI 优化高native-*, bundle-*
4原生性能高native-*
5内存管理中高js-*, native-*
6动画中js-*

影响标签是分类提示:优先处理关键问题,其次是高优先级,当有证据指向时再考虑中优先级。

快速参考

优化工作流程

对于任何性能问题,遵循此循环:测量 → 优化 → 重新测量 → 验证

  1. 测量:在更改前捕获基线指标。对于运行时问题,优先使用提交时间线、重渲染次数、缓慢组件、最重提交分解、启动/TTI(可用时)。组件树深度或数量是可选的上下文,不能替代指标。在没有测量的渲染或 FPS 问题的情况下,不要推荐 memoization、原子状态或编译器更改。
  2. 优化:从相关参考中应用有针对性的修复
  3. 重新测量:运行相同的测量以获取更新后的指标
  4. 验证:确认改进(例如,FPS 从 45 提升到 60,TTI 从 3.2 秒降低到 1.8 秒,包体积从 2.1 MB 减小到 1.6 MB)

如果指标没有改善,回滚并尝试下一个建议的修复。

审查护栏

-在提出特定 API 的修复之前检查库版本。例如:FlashList v2 废弃了 estimatedItemSize,所以不要将其标记为缺失。

  • 除非行为明显不正确,或者性能分析显示有与该值相关的浪费工作,否则不要建议更改 useMemo 或 useCallback 的依赖项。
  • 不要推测性地报告过时闭包。在指出之前,展示过时读取路径、复现步骤或性能分析器证据。
  • 在分析流程时,测量目标交互本身。不要将组件树深度或组件数量作为主要性能证据。

关键:FPS 与重渲染

先进行性能分析:

agent-device react-devtools status
agent-device react-devtools wait --connected
agent-device react-devtools profile start
agent-device react-devtools profile stop
agent-device react-devtools profile slow --limit 5
agent-device react-devtools profile rerenders --limit 5
agent-device react-devtools profile timeline --limit 20

在 profile start 和 profile stop 之间使用正常的 agent-device 命令驱动目标交互。

当 agent-device 不可用时的手动备选方案:通过 Metro (j) 或开发菜单打开 React Native DevTools,使用 Profiler 选项卡,并记录相同的交互。

对于发布构建的 React 组件性能分析,首先连接 @callstack/inspector,以便 React DevTools 可以附加到发布应用,然后运行上述的 agent-device react-devtools 流程。

常见修复:

  • 对于长列表,将 ScrollView 替换为 FlatList/FlashList/Legend List
  • 在性能分析显示级联重渲染后,使用 React Compiler 进行自动 memoization
  • 在性能分析显示广泛的 store/context 更新后,使用原子状态(Jotai/Zustand)减少重渲染
  • 对昂贵计算使用 useDeferredValue

关键:包体积

分析包:

npx react-native bundle \
  --entry-file index.js \
  --bundle-output output.js \
  --platform ios \
  --sourcemap-output output.js.map \
  --dev false --minify true

npx source-map-explorer output.js --no-border-checks

优化后验证改进:

# 在更改前记录基线大小
ls -lh output.js  # 例如,之前:2.1 MB

# 应用修复后,重新打包并比较
npx react-native bundle --entry-file index.js --bundle-output output.js \
  --platform ios --dev false --minify true
ls -lh output.js  # 例如,之后:1.6 MB(减少 24%)

常见修复:

  • 避免桶形导入(直接从源文件导入)
  • 仅在检查 Hermes API 和方法覆盖后移除不必要的 Intl polyfill
  • 评估 tree shaking(Expo SDK 52+ 实验性未使用导入/导出移除,或仅当已配置时使用 Re.Pack)
  • 启用 R8 进行 Android 原生代码缩减

高:TTI 优化

测量 TTI:

  • 使用 react-native-performance 获取标记
  • 仅测量冷启动(排除热/温/预启动)

常见修复:

  • 对于 React Native 0.78 及更早版本,禁用 Android JS 包压缩以启用 Hermes mmap
  • 使用原生导航(react-native-screens)
  • 在导航到之前预加载常用的昂贵屏幕

高:原生性能

分析原生层:

  • iOS:Xcode Instruments → Time Profiler
  • Android:Android Studio → CPU Profiler

常见修复:

  • 为繁重的原生工作使用后台线程
  • 优先使用异步 Turdo Module 方法而非同步方法
  • 使用 C++ 处理跨平台性能关键代码

参考资料

参考资料 中包含完整文档和代码示例:

JavaScript/React (js-*)

文件影响描述
js-lists-flatlist-flashlist.md关键使用虚拟化列表替换 ScrollView
js-profile-react.md中agent-device react-devtools 性能分析
js-measure-fps.md高FPS 监控和测量
js-memory-leaks.md中JS 内存泄漏排查
js-atomic-state.md高Jotai/Zustand 模式
js-concurrent-react.md高useDeferredValue, useTransition
js-react-compiler.md高自动 memoization
js-animations-reanimated.md中Reanimated worklets
js-bottomsheet.md高底部弹窗优化
js-uncontrolled-components.md高TextInput 优化

原生 (native-*)

文件影响描述
native-turbo-modules.md高构建快速的原生模块
native-sdks-over-polyfills.md高原生库 vs JS 库
native-measure-tti.md高TTI 测量设置
native-threading-model.md高Turdo Module 线程
native-profiling.md中Xcode/Android Studio 性能分析
native-platform-setup.md中iOS/Android 工具指南
native-view-flattening.md中视图层级调试
native-memory-patterns.md中C++/Swift/Kotlin 内存
native-memory-leaks.md中原生内存泄漏排查
native-android-16kb-alignment.md关键针对 Google Play 的第三方库对齐

打包 (bundle-*)

文件影响描述
bundle-barrel-exports.md关键避免桶形导入
bundle-analyze-js.md关键JS 包可视化
bundle-tree-shaking.md高死代码消除
bundle-analyze-app.md高应用体积分析
bundle-r8-android.md高Android 代码缩减
bundle-hermes-mmap.md高禁用包压缩
bundle-native-assets.md高资源目录设置
bundle-library-size.md中评估依赖项
bundle-code-splitting.md中远程代码块加载保护措施

问题→技能映射

问题从何处开始
应用感觉缓慢/卡顿js-measure-fps.md → js-profile-react.md
重渲染过多js-profile-react.md → js-react-compiler.md
启动慢(TTI)native-measure-tti.md → bundle-analyze-js.md
应用体积大bundle-analyze-app.md → bundle-r8-android.md
内存增长js-memory-leaks.md 或 native-memory-leaks.md
动画丢帧js-animations-reanimated.md
底部弹窗卡顿/重渲染js-bottomsheet.md → js-animations-reanimated.md
列表滚动卡顿js-lists-flatlist-flashlist.md
TextInput 延迟js-uncontrolled-components.md
原生模块慢native-turbo-modules.md → native-threading-model.md
原生库对齐问题native-android-16kb-alignment.md

出处

基于 Callstack 的“React Native 优化终极指南”。

Individual skills in this repo

This repo contains 15 individual skills — each has its own dedicated page.

agent-device

Automates interactions for Apple-platform apps (iOS, tvOS, macOS) and Android devices. Use when navigating apps, taking snapshots/screenshots, tapping, typing, scrolling, or extracting UI info across mobile, TV, and desktop targets.

assess-react-native-migration

Assesses whether and how an existing mobile product should migrate to React Native. Use when auditing one or more product repositories for migration readiness, including products whose iOS, Android, and other clients live in separate directories or repositories; choosing brownfield, greenfield, or a checkpoint-based path; defining a representative trial; or preparing a baseline and ROI decision before implementation. When product scope or material evidence is unavailable, grills the stakeholder with exactly one question per turn instead of sending a questionnaire.

callstackincubator/github

GitHub patterns using gh CLI for pull requests, stacked PRs, code review, branching strategies, and repository automation. Use when working with GitHub PRs, merging strategies, or repository management tasks.

callstackincubator/validate-skills

Validates skills in this repo against agentskills.io spec and Claude Code best practices. Use via /validate-skills command.

create-react-native-library

Scaffolds React Native libraries with create-react-native-library for standalone libraries or local native modules and views. Use when creating or working on React Native libraries or adding native functionality in an existing app.

dogfood

Systematically explore and test a mobile app on iOS/Android with agent-device to find bugs, UX issues, and other problems. Use when asked to "dogfood", "QA", "exploratory test", "find issues", "bug hunt", or "test this app" on mobile. Produces a structured report with reproducible evidence: screenshots, optional repro videos, and detailed steps for every issue.

github-actions

GitHub Actions workflow patterns for React Native iOS simulator and Android emulator cloud builds with downloadable artifacts. Use when setting up CI build pipelines or downloading GitHub Actions artifacts via gh CLI and GitHub API.

react-native-brownfield-migration

Implements an accepted incremental brownfield migration from native iOS or Android to React Native or Expo using @callstack/react-native-brownfield. Use after the brownfield path has been selected, when setting up the integration, packaging XCFramework or AAR artifacts, or adding React Native surfaces to native hosts.

react-native-testing

Write tests using React Native Testing Library (RNTL) v13 and v14 (`@testing-library/react-native`). Use when writing, reviewing, or fixing React Native component tests. Covers: render, screen, queries (getBy/getAllBy/queryBy/findBy), Jest matchers, userEvent, fireEvent, waitFor, and async patterns. Supports v13 (React 18, sync render) and v14 (React 19+, async render). Triggers on: test files for React Native components, RNTL imports, mentions of "testing library", "write tests", "component tests", or "RNTL".

react-native-tv-best-practices

Reviews React Native TV apps for focus/D-pad navigation, 10-foot UI layout, TV playback/DRM integration, low-memory TV performance, and TV accessibility. Use when building, debugging, or reviewing react-native-tvos, Expo TV, Amazon Vega/Kepler, or React Native web TV targets where the issue depends on remote input, TV focus, TV packaging, TV hardware, or TV playback constraints.

react-navigation

Provides React Navigation UI patterns for stacks, tabs, drawers etc. Use when building navigation UIs with React Navigation, configuring headers, bottom sheets or handling safe areas and insets.

upgrading-react-native

Upgrades React Native apps to newer versions by applying rn-diff-purge template diffs, updating package.json dependencies, migrating native iOS and Android configuration, resolving CocoaPods and Gradle changes, and handling breaking API updates. Use when upgrading React Native, bumping RN version, updating from RN 0.x to 0.y, or migrating Expo SDK alongside a React Native upgrade.

validate-skills

Validates skills in this repo against agentskills.io spec and Claude Code best practices. Use via /validate-skills command.

vercel-react-native-skills

React Native and Expo best practices for building performant mobile apps. Use when building React Native components, optimizing list performance, implementing animations, or working with native modules. Triggers on tasks involving React Native, Expo, mobile performance, or native platform APIs.

writing-user-docs

House style for user-facing documentation — voice, scope, structure, and what to leave out. Use this skill whenever writing, editing, or reviewing anything a user reads to learn how to use a tool — help center articles, getting-started guides, tutorials, feature docs, README usage sections, in-app help, release notes, or FAQ entries. Trigger it even when the request is phrased plainly — "document this feature", "write docs for X", "explain this to users", "write a README for this library", "turn these notes into a guide" — and even when no style guidelines are mentioned. Also use it to review existing docs for tone, bloat, or leaked implementation detail. Do not use it for internal engineering docs, architecture write-ups, RFCs, or code comments.

相关技能