React Native 技巧
React Native 和 Expo 應用程式的全面最佳實務。包含涵蓋效能、動畫、UI 模式及平台特定優化等多個類別的規則。
適用時機
在以下情況參考這些指南:
- 建構 React Native 或 Expo 應用程式
- 優化列表與滾動效能
- 使用 Reanimated 實作動畫
- 處理圖片與多媒體
- 配置原生模組或字型
- 建構包含原生相依項目的 monorepo 專案
按優先級分類的規則類別
| 優先級 | 類別 | 影響 | 前綴 |
|---|---|---|---|
| 1 | 列表效能 | 關鍵 | list-performance- |
| 2 | 動畫 | 高 | animation- |
| 3 | 導航 | 高 | navigation- |
| 4 | UI 模式 | 高 | ui- |
| 5 | 狀態管理 | 中 | react-state- |
| 6 | 渲染 | 中 | rendering- |
| 7 | Monorepo | 中 | monorepo- |
| 8 | 配置 | 低 | fonts-, imports- |
快速參考
1. 列表效能 (關鍵)
list-performance-virtualize- 對大型列表使用 FlashListlist-performance-item-memo- 記憶化列表項目元件list-performance-callbacks- 穩定化回呼參考list-performance-inline-objects- 避免行內樣式物件list-performance-function-references- 將函式提取到渲染外部list-performance-images- 優化列表中的圖片list-performance-item-expensive- 將耗費效能的運算移出項目list-performance-item-types- 對異質列表使用項目類型
2. 動畫 (高)
animation-gpu-properties- 僅對 transform 和 opacity 進行動畫animation-derived-value- 使用 useDerivedValue 處理計算後的動畫animation-gesture-detector-press- 使用 Gesture.Tap 代替 Pressable
3. 導航 (高)
navigation-native-navigators- 優先使用原生堆疊和原生標籤導航器,而非 JS 導航器
4. UI 模式 (高)
ui-expo-image- 對所有圖片使用 expo-imageui-image-gallery- 使用 Galeria 實作圖片燈箱ui-pressable- 使用 Pressable 代替 TouchableOpacityui-safe-area-scroll- 在 ScrollView 中處理安全區域ui-scrollview-content-inset- 對頁首使用 contentInsetui-menus- 使用原生上下文選單ui-native-modals- 盡可能使用原生模態視窗ui-measure-views- 使用 onLayout,而非 measure()ui-styling- 使用 StyleSheet.create 或 Nativewind
5. 狀態管理 (中)
react-state-minimize- 最小化狀態訂閱react-state-dispatcher- 對回呼使用 dispatcher 模式react-state-fallback- 在首次渲染時顯示備用內容react-compiler-destructure-functions- 為 React Compiler 進行解構react-compiler-reanimated-shared-values- 使用 Compiler 處理共享值
6. 渲染 (中)
rendering-text-in-text-component- 在 Text 元件中包裹文字rendering-no-falsy-and- 避免使用 falsy && 進行條件渲染
7. Monorepo (中)
monorepo-native-deps-in-app- 將原生相依項目保留在應用程式套件中monorepo-single-dependency-versions- 在各套件中使用單一版本
8. 配置 (低)
fonts-config-plugin- 使用配置插件處理自訂字型imports-design-system-folder- 組織設計系統的匯入js-hoist-intl- 提升 Intl 物件的建立位置
如何使用
閱讀個別規則檔案以取得詳細說明和程式碼範例:
rules/list-performance-virtualize.md
rules/animation-gpu-properties.md
每個規則檔案包含:
- 簡要說明其重要性
- 錯誤的程式碼範例與說明
- 正確的程式碼範例與說明
- 其他相關背景資訊與參考資料
完整合併文件
如需包含所有規則的完整指南:AGENTS.md