Community程式設計與開發github.com

react-native-best-practices

提供 React Native 效能優化指南,涵蓋 FPS、TTI、Bundle 大小、記憶體洩漏、重新渲染與動畫。適用於 Hermes 優化、JS 執行緒阻塞、Bridge 負載、FlashList、原生模組或除錯卡頓與掉幀等任務。

react-native-best-practices 是什麼?

這是一份為開發者與 AI 代理設計的 React Native 效能優化技能,提供一套完整且優先級排序的指南,幫助診斷與解決應用程式中的常見效能瓶頸。技能涵蓋 JavaScript/React 層、原生 (iOS/Android) 層以及打包 (Bundling) 層的優化策略,並根據影響程度分為「關鍵」、「高」與「中等」三個優先級,讓使用者能夠快速定位並解決最關鍵的問題。 此技能支援所有基於 React Native 的開發與除錯平台,包括 Claude Code、Cursor 與 Codex 等 AI 代理工具。其核心運作方式遵循「測量 → 優化 → 再測量 → 驗證」的迭代流程,強調以數據驅動的決策,避免在沒有實際測量結果的情況下進行猜測性的優化。技能內建了詳細的參考文件對應表,從「應用程式感覺卡頓」、「過多重新渲染」、「啟動時間過長」等常見問題出發,引導使用者直接連結到最相關的優化指南與範例程式碼。 此外,此技能還包含了安全準則,提醒使用者在執行 Shell 指令、整合第三方套件或進行遠端程式碼分割時,應遵循的最佳實務與風險控管措施。無論是新手還是經驗豐富的開發者,都能透過這份技能系統性地提升 React Native 應用程式的效能。

相容平台~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)
  • 減少 Bundle 或應用程式大小
  • 編寫原生模組 (Turbo Modules)
  • 分析 React Native 效能
  • 審查 React Native 程式碼的效能

安全注意事項

  • 將這些參考資料中的 Shell 指令視為本地開發者操作。執行前請務必審查,優先使用固定版本的工具,並避免將遠端腳本直接 Pipe 到 Shell。
  • 將第三方函式庫與外掛視為仍需一般供應鏈控制的相依性:鎖定版本、驗證來源,並透過標準審查流程進行更新。
  • 將遠端區塊載入僅視為第一方成品傳遞。優先使用應用程式內建的區塊或已簽署的 CI 發布清單;託管的區塊必須來自您控制的、受信任的 HTTPS 來源,並鎖定在當前應用程式版本。

按優先順序的指南

優先級類別影響前綴
1FPS 與重新渲染關鍵js-*
2Bundle 大小關鍵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秒,Bundle 2.1MB→1.6MB)

如果指標沒有改善,請還原並嘗試下一個建議的修復。

審查護欄

  • 在建議特定 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) 標籤頁,並記錄相同的互動。

對於 Release Build 的 React 元件分析,請先連線 @callstack/inspector,以便 React DevTools 可以附加到 Release 應用程式,然後執行上述的 agent-device react-devtools 流程。

常見修復:

  • 對於長列表,將 ScrollView 替換為 FlatList/FlashList/Legend List
  • 在分析顯示級聯重新渲染後,使用 React Compiler 進行自動 memoization
  • 在分析顯示廣泛的 Store/Context 更新後,使用原子狀態 (Jotai/Zustand) 來減少重新渲染
  • 對於昂貴的計算,使用 useDeferredValue

關鍵:Bundle 大小

分析 Bundle:

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  # 例如:Before: 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  # 例如:After: 1.6 MB  (減少 24%)

常見修復:

  • 避免 Barrel Imports(直接從來源匯入)
  • 僅在檢查 Hermes API 和方法覆蓋率後,才移除不必要的 Intl Polyfill
  • 評估 Tree Shaking(Expo SDK 52+ 實驗性的未使用 Import/Export 移除,或僅在已配置時使用 Re.Pack)
  • 啟用 R8 以進行 Android 原生程式碼縮減

高:TTI 優化

測量 TTI:

  • 使用 react-native-performance 作為標記
  • 僅測量冷啟動(排除熱/暖/預熱啟動)

常見修復:

  • 對於 React Native 0.78 及更早版本,停用 Android JS Bundle 壓縮以啟用 Hermes mmap
  • 使用原生導航 (react-native-screens)
  • 在導航到常用且昂貴的畫面之前,預先載入它們

高:原生效能

分析原生:

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

常見修復:

  • 對繁重的原生工作使用背景執行緒
  • 偏好使用非同步而非同步的 Turbo Module 方法
  • 對於跨平台效能關鍵程式碼,使用 C++

參考資料

完整文件與程式碼範例位於 references/:

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高Bottom Sheet 優化
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高Turbo Module 執行緒
native-profiling.md中等Xcode/Android Studio 分析
native-platform-setup.md中等iOS/Android 工具指南
native-view-flattening.md中等View 階層除錯
native-memory-patterns.md中等C++/Swift/Kotlin 記憶體
native-memory-leaks.md中等原生記憶體洩漏偵測
native-android-16kb-alignment.md關鍵為 Google Play 進行第三方函式庫對齊

打包 (bundle-*)

檔案影響描述
bundle-barrel-exports.md關鍵避免 Barrel Imports
bundle-analyze-js.md關鍵JS Bundle 視覺化
bundle-tree-shaking.md高死碼消除
bundle-analyze-app.md高應用程式大小分析
bundle-r8-android.md高Android 程式碼縮減
bundle-hermes-mmap.md高停用 Bundle 壓縮
bundle-native-assets.md高Asset 目錄設定
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
Bottom Sheet 卡頓/重新渲染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.

相關技能