react-native-best-practices

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

React Native Best Practices

React Native 性能优化最佳实践

Overview

概述

Performance optimization guide for React Native applications, covering JavaScript/React, Native (iOS/Android), and bundling optimizations. Based on Callstack's "Ultimate Guide to React Native Optimization".
React Native应用性能优化指南,涵盖JavaScript/React、原生(iOS/Android)和打包优化。基于Callstack的《React Native优化终极指南》。

Skill Format

技能格式

Each reference file follows a hybrid format for fast lookup and deep understanding:
  • Quick Pattern: Incorrect/Correct code snippets for immediate pattern matching
  • Quick Command: Shell commands for process/measurement skills
  • Quick Config: Configuration snippets for setup-focused skills
  • Quick Reference: Summary tables for conceptual skills
  • Deep Dive: Full context with When to Use, Prerequisites, Step-by-Step, Common Pitfalls
Impact ratings: CRITICAL (fix immediately), HIGH (significant improvement), MEDIUM (worthwhile optimization)
每个参考文件采用混合格式,便于快速查找和深入理解:
  • 快速模式:错误/正确代码片段,用于即时匹配模式
  • 快速命令:用于流程/测量技能的Shell命令
  • 快速配置:用于设置类技能的配置片段
  • 快速参考:用于概念类技能的汇总表格
  • 深入解析:包含适用场景、前提条件、分步指南、常见陷阱的完整内容
影响等级:CRITICAL(立即修复)、HIGH(显著提升)、MEDIUM(值得优化)

When to Apply

适用场景

Reference these guidelines when:
  • Debugging slow/janky UI or animations
  • Investigating memory leaks (JS or native)
  • Optimizing app startup time (TTI)
  • Reducing bundle or app size
  • Writing native modules (Turbo Modules)
  • Profiling React Native performance
  • Reviewing React Native code for performance
在以下场景中参考本指南:
  • 调试缓慢/卡顿的UI或动画
  • 排查内存泄漏问题(JS或原生)
  • 优化应用启动时间(TTI)
  • 减小包或应用体积
  • 编写原生模块(Turbo Modules)
  • 分析React Native性能
  • 评审React Native代码的性能

Priority-Ordered Guidelines

优先级排序的优化指南

PriorityCategoryImpactPrefix
1FPS & Re-rendersCRITICAL
js-*
2Bundle SizeCRITICAL
bundle-*
3TTI OptimizationHIGH
native-*
,
bundle-*
4Native PerformanceHIGH
native-*
5Memory ManagementMEDIUM-HIGH
js-*
,
native-*
6AnimationsMEDIUM
js-*
优先级分类影响等级前缀
1FPS与重渲染CRITICAL
js-*
2包体积CRITICAL
bundle-*
3TTI优化HIGH
native-*
,
bundle-*
4原生性能HIGH
native-*
5内存管理MEDIUM-HIGH
js-*
,
native-*
6动画MEDIUM
js-*

Quick Reference

快速参考

Critical: FPS & Re-renders

关键:FPS与重渲染

Profile first:
bash
undefined
先分析性能:
bash
undefined

Open React Native DevTools

打开React Native DevTools

Press 'j' in Metro, or shake device → "Open DevTools"

在Metro中按'j',或摇晃设备 → "Open DevTools"


**Common fixes:**
- Replace ScrollView with FlatList/FlashList for lists
- Use React Compiler for automatic memoization
- Use atomic state (Jotai/Zustand) to reduce re-renders
- Use `useDeferredValue` for expensive computations

**常见修复方案:**
- 用FlatList/FlashList替代ScrollView实现列表
- 使用React Compiler自动进行记忆化处理
- 使用原子状态(Jotai/Zustand)减少重渲染
- 用`useDeferredValue`处理昂贵的计算

Critical: Bundle Size

关键:包体积

Analyze bundle:
bash
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
Common fixes:
  • Avoid barrel imports (import directly from source)
  • Remove unnecessary Intl polyfills (Hermes has native support)
  • Enable tree shaking (Expo SDK 52+ or Re.Pack)
  • Enable R8 for Android native code shrinking
分析包内容:
bash
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
常见修复方案:
  • 避免桶式导入(直接从源文件导入)
  • 移除不必要的Intl polyfills(Hermes原生支持)
  • 启用摇树优化(Expo SDK 52+ 或 Re.Pack)
  • 为Android原生代码启用R8压缩

High: TTI Optimization

重要:TTI优化

Measure TTI:
  • Use
    react-native-performance
    for markers
  • Only measure cold starts (exclude warm/hot/prewarm)
Common fixes:
  • Disable JS bundle compression on Android (enables Hermes mmap)
  • Use native navigation (react-native-screens)
  • Preload commonly-used expensive screens before navigating to them
测量TTI:
  • 使用
    react-native-performance
    添加标记
  • 仅测量冷启动(排除暖启动/热启动/预启动)
常见修复方案:
  • 在Android上禁用JS包压缩(启用Hermes内存映射)
  • 使用原生导航(react-native-screens)
  • 在导航前预加载常用的复杂页面

High: Native Performance

重要:原生性能

Profile native:
  • iOS: Xcode Instruments → Time Profiler
  • Android: Android Studio → CPU Profiler
Common fixes:
  • Use background threads for heavy native work
  • Prefer async over sync Turbo Module methods
  • Use C++ for cross-platform performance-critical code
分析原生性能:
  • iOS:Xcode Instruments → Time Profiler
  • Android:Android Studio → CPU Profiler
常见修复方案:
  • 用后台线程处理繁重的原生任务
  • 优先选择异步而非同步的Turbo Module方法
  • 用C++编写跨平台的性能关键代码

References

参考资料

Full documentation with code examples in [references/][references]:
包含代码示例的完整文档见 [references/][references]:

JavaScript/React (
js-*
)

JavaScript/React (
js-*
)

FileImpactDescription
[js-lists-flatlist-flashlist.md][js-lists-flatlist-flashlist]CRITICALReplace ScrollView with virtualized lists
[js-profile-react.md][js-profile-react]MEDIUMReact DevTools profiling
[js-measure-fps.md][js-measure-fps]HIGHFPS monitoring and measurement
[js-memory-leaks.md][js-memory-leaks]MEDIUMJS memory leak hunting
[js-atomic-state.md][js-atomic-state]HIGHJotai/Zustand patterns
[js-concurrent-react.md][js-concurrent-react]HIGHuseDeferredValue, useTransition
[js-react-compiler.md][js-react-compiler]HIGHAutomatic memoization
[js-animations-reanimated.md][js-animations-reanimated]MEDIUMReanimated worklets
[js-uncontrolled-components.md][js-uncontrolled-components]HIGHTextInput optimization
文件影响等级描述
[js-lists-flatlist-flashlist.md][js-lists-flatlist-flashlist]CRITICAL用虚拟化列表替代ScrollView
[js-profile-react.md][js-profile-react]MEDIUMReact DevTools性能分析
[js-measure-fps.md][js-measure-fps]HIGHFPS监控与测量
[js-memory-leaks.md][js-memory-leaks]MEDIUMJS内存泄漏排查
[js-atomic-state.md][js-atomic-state]HIGHJotai/Zustand使用模式
[js-concurrent-react.md][js-concurrent-react]HIGHuseDeferredValue、useTransition
[js-react-compiler.md][js-react-compiler]HIGH自动记忆化处理
[js-animations-reanimated.md][js-animations-reanimated]MEDIUMReanimated工作流
[js-uncontrolled-components.md][js-uncontrolled-components]HIGHTextInput优化

Native (
native-*
)

原生 (
native-*
)

FileImpactDescription
[native-turbo-modules.md][native-turbo-modules]HIGHBuilding fast native modules
[native-sdks-over-polyfills.md][native-sdks-over-polyfills]HIGHNative vs JS libraries
[native-measure-tti.md][native-measure-tti]HIGHTTI measurement setup
[native-threading-model.md][native-threading-model]HIGHTurbo Module threads
[native-profiling.md][native-profiling]MEDIUMXcode/Android Studio profiling
[native-platform-setup.md][native-platform-setup]MEDIUMiOS/Android tooling guide
[native-view-flattening.md][native-view-flattening]MEDIUMView hierarchy debugging
[native-memory-patterns.md][native-memory-patterns]MEDIUMC++/Swift/Kotlin memory
[native-memory-leaks.md][native-memory-leaks]MEDIUMNative memory leak hunting
[native-android-16kb-alignment.md][native-android-16kb-alignment]CRITICALThird-party library alignment for Google Play
文件影响等级描述
[native-turbo-modules.md][native-turbo-modules]HIGH构建高性能原生模块
[native-sdks-over-polyfills.md][native-sdks-over-polyfills]HIGH原生库与JS库的选择
[native-measure-tti.md][native-measure-tti]HIGHTTI测量设置
[native-threading-model.md][native-threading-model]HIGHTurbo Module线程模型
[native-profiling.md][native-profiling]MEDIUMXcode/Android Studio性能分析
[native-platform-setup.md][native-platform-setup]MEDIUMiOS/Android工具指南
[native-view-flattening.md][native-view-flattening]MEDIUM视图层级调试
[native-memory-patterns.md][native-memory-patterns]MEDIUMC++/Swift/Kotlin内存管理
[native-memory-leaks.md][native-memory-leaks]MEDIUM原生内存泄漏排查
[native-android-16kb-alignment.md][native-android-16kb-alignment]CRITICAL适配Google Play的第三方库16KB对齐要求

Bundling (
bundle-*
)

打包 (
bundle-*
)

FileImpactDescription
[bundle-barrel-exports.md][bundle-barrel-exports]CRITICALAvoid barrel imports
[bundle-analyze-js.md][bundle-analyze-js]CRITICALJS bundle visualization
[bundle-tree-shaking.md][bundle-tree-shaking]HIGHDead code elimination
[bundle-analyze-app.md][bundle-analyze-app]HIGHApp size analysis
[bundle-r8-android.md][bundle-r8-android]HIGHAndroid code shrinking
[bundle-hermes-mmap.md][bundle-hermes-mmap]HIGHDisable bundle compression
[bundle-native-assets.md][bundle-native-assets]HIGHAsset catalog setup
[bundle-library-size.md][bundle-library-size]MEDIUMEvaluate dependencies
[bundle-code-splitting.md][bundle-code-splitting]MEDIUMRe.Pack code splitting
文件影响等级描述
[bundle-barrel-exports.md][bundle-barrel-exports]CRITICAL避免桶式导出
[bundle-analyze-js.md][bundle-analyze-js]CRITICALJS包可视化分析
[bundle-tree-shaking.md][bundle-tree-shaking]HIGH无用代码消除
[bundle-analyze-app.md][bundle-analyze-app]HIGH应用体积分析
[bundle-r8-android.md][bundle-r8-android]HIGHAndroid代码压缩
[bundle-hermes-mmap.md][bundle-hermes-mmap]HIGH禁用包压缩
[bundle-native-assets.md][bundle-native-assets]HIGH资源目录设置
[bundle-library-size.md][bundle-library-size]MEDIUM依赖库体积评估
[bundle-code-splitting.md][bundle-code-splitting]MEDIUMRe.Pack代码拆分

Searching References

查找参考资料

bash
undefined
bash
undefined

Find patterns by keyword

按关键词查找相关模式

grep -l "reanimated" references/ grep -l "flatlist" references/ grep -l "memory" references/ grep -l "profil" references/ grep -l "tti" references/ grep -l "bundle" references/
undefined
grep -l "reanimated" references/ grep -l "flatlist" references/ grep -l "memory" references/ grep -l "profil" references/ grep -l "tti" references/ grep -l "bundle" references/
undefined

Problem → Skill Mapping

问题→技能映射

ProblemStart With
App feels slow/jankyjs-measure-fps.mdjs-profile-react.md
Too many re-rendersjs-profile-react.mdjs-react-compiler.md
Slow startup (TTI)native-measure-tti.mdbundle-analyze-js.md
Large app sizebundle-analyze-app.mdbundle-r8-android.md
Memory growingjs-memory-leaks.md or native-memory-leaks.md
Animation drops framesjs-animations-reanimated.md
List scroll jankjs-lists-flatlist-flashlist.md
TextInput lagjs-uncontrolled-components.md
Native module slownative-turbo-modules.mdnative-threading-model.md
Native library alignment issuenative-android-16kb-alignment.md
问题从以下开始排查
应用感觉缓慢/卡顿js-measure-fps.mdjs-profile-react.md
重渲染过多js-profile-react.mdjs-react-compiler.md
启动缓慢(TTI)native-measure-tti.mdbundle-analyze-js.md
应用体积过大bundle-analyze-app.mdbundle-r8-android.md
内存持续增长js-memory-leaks.mdnative-memory-leaks.md
动画掉帧js-animations-reanimated.md
列表滚动卡顿js-lists-flatlist-flashlist.md
TextInput响应延迟js-uncontrolled-components.md
原生模块缓慢native-turbo-modules.mdnative-threading-model.md
原生库对齐问题native-android-16kb-alignment.md

Attribution

致谢

Based on "The Ultimate Guide to React Native Optimization" by Callstack.
基于Callstack的《The Ultimate Guide to React Native Optimization》改编。