react-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseVercel React Best Practices
Vercel React 最佳实践
Comprehensive performance optimization guide for React and Next.js applications, maintained by Vercel. Contains 45 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
这是由Vercel维护的React和Next.js应用性能优化综合指南,包含8个类别下的45条规则,按影响优先级排序,可指导自动化重构与代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new React components or Next.js pages
- Implementing data fetching (client or server-side)
- Reviewing code for performance issues
- Refactoring existing React/Next.js code
- Optimizing bundle size or load times
在以下场景中可参考本指南:
- 编写新的React组件或Next.js页面
- 实现数据获取(客户端或服务端)
- 评审代码以排查性能问题
- 重构现有React/Next.js代码
- 优化包体积或加载时间
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Eliminating Waterfalls | CRITICAL | |
| 2 | Bundle Size Optimization | CRITICAL | |
| 3 | Server-Side Performance | HIGH | |
| 4 | Client-Side Data Fetching | MEDIUM-HIGH | |
| 5 | Re-render Optimization | MEDIUM | |
| 6 | Rendering Performance | MEDIUM | |
| 7 | JavaScript Performance | LOW-MEDIUM | |
| 8 | Advanced Patterns | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 消除请求瀑布流 | 关键 | |
| 2 | 包体积优化 | 关键 | |
| 3 | 服务端性能 | 高 | |
| 4 | 客户端数据获取 | 中高 | |
| 5 | 重渲染优化 | 中 | |
| 6 | 渲染性能 | 中 | |
| 7 | JavaScript性能 | 中低 | |
| 8 | 高级模式 | 低 | |
Quick Reference
快速参考
1. Eliminating Waterfalls (CRITICAL)
1. 消除请求瀑布流(关键)
- - Move await into branches where actually used
async-defer-await - - Use Promise.all() for independent operations
async-parallel - - Use better-all for partial dependencies
async-dependencies - - Start promises early, await late in API routes
async-api-routes - - Use Suspense to stream content
async-suspense-boundaries
- - 将await移至实际需要使用的分支中
async-defer-await - - 对独立操作使用Promise.all()
async-parallel - - 对部分依赖使用better-all
async-dependencies - - 在API路由中尽早启动Promise,延迟await
async-api-routes - - 使用Suspense流式传输内容
async-suspense-boundaries
2. Bundle Size Optimization (CRITICAL)
2. 包体积优化(关键)
- - Import directly, avoid barrel files
bundle-barrel-imports - - Use next/dynamic for heavy components
bundle-dynamic-imports - - Load analytics/logging after hydration
bundle-defer-third-party - - Load modules only when feature is activated
bundle-conditional - - Preload on hover/focus for perceived speed
bundle-preload
- - 直接导入,避免桶文件
bundle-barrel-imports - - 对重型组件使用next/dynamic
bundle-dynamic-imports - - 在 hydration 后加载分析/日志工具
bundle-defer-third-party - - 仅在功能激活时加载模块
bundle-conditional - - 在 hover/focus 时预加载以提升感知速度
bundle-preload
3. Server-Side Performance (HIGH)
3. 服务端性能(高)
- - Use React.cache() for per-request deduplication
server-cache-react - - Use LRU cache for cross-request caching
server-cache-lru - - Minimize data passed to client components
server-serialization - - Restructure components to parallelize fetches
server-parallel-fetching - - Use after() for non-blocking operations
server-after-nonblocking
- - 使用React.cache()实现请求内去重
server-cache-react - - 使用LRU缓存实现跨请求缓存
server-cache-lru - - 减少传递给客户端组件的数据量
server-serialization - - 重构组件以并行化数据获取
server-parallel-fetching - - 对非阻塞操作使用after()
server-after-nonblocking
4. Client-Side Data Fetching (MEDIUM-HIGH)
4. 客户端数据获取(中高)
- - Use SWR for automatic request deduplication
client-swr-dedup - - Deduplicate global event listeners
client-event-listeners
- - 使用SWR实现自动请求去重
client-swr-dedup - - 对全局事件监听器进行去重
client-event-listeners
5. Re-render Optimization (MEDIUM)
5. 重渲染优化(中)
- - Don't subscribe to state only used in callbacks
rerender-defer-reads - - Extract expensive work into memoized components
rerender-memo - - Use primitive dependencies in effects
rerender-dependencies - - Subscribe to derived booleans, not raw values
rerender-derived-state - - Use functional setState for stable callbacks
rerender-functional-setstate - - Pass function to useState for expensive values
rerender-lazy-state-init - - Use startTransition for non-urgent updates
rerender-transitions
- - 不要订阅仅在回调中使用的状态
rerender-defer-reads - - 将昂贵的操作提取到 memoized 组件中
rerender-memo - - 在effects中使用原始类型依赖
rerender-dependencies - - 订阅派生的布尔值,而非原始值
rerender-derived-state - - 使用函数式setState以获取稳定回调
rerender-functional-setstate - - 对昂贵的值,向useState传递函数
rerender-lazy-state-init - - 使用startTransition处理非紧急更新
rerender-transitions
6. Rendering Performance (MEDIUM)
6. 渲染性能(中)
- - Animate div wrapper, not SVG element
rendering-animate-svg-wrapper - - Use content-visibility for long lists
rendering-content-visibility - - Extract static JSX outside components
rendering-hoist-jsx - - Reduce SVG coordinate precision
rendering-svg-precision - - Use inline script for client-only data
rendering-hydration-no-flicker - - Use Activity component for show/hide
rendering-activity - - Use ternary, not && for conditionals
rendering-conditional-render
- - 为div包装器添加动画,而非SVG元素
rendering-animate-svg-wrapper - - 对长列表使用content-visibility
rendering-content-visibility - - 将静态JSX提取到组件外部
rendering-hoist-jsx - - 降低SVG坐标精度
rendering-svg-precision - - 使用内联脚本处理仅客户端数据
rendering-hydration-no-flicker - - 使用Activity组件实现显示/隐藏
rendering-activity - - 使用三元表达式而非&&进行条件渲染
rendering-conditional-render
7. JavaScript Performance (LOW-MEDIUM)
7. JavaScript性能(中低)
- - Group CSS changes via classes or cssText
js-batch-dom-css - - Build Map for repeated lookups
js-index-maps - - Cache object properties in loops
js-cache-property-access - - Cache function results in module-level Map
js-cache-function-results - - Cache localStorage/sessionStorage reads
js-cache-storage - - Combine multiple filter/map into one loop
js-combine-iterations - - Check array length before expensive comparison
js-length-check-first - - Return early from functions
js-early-exit - - Hoist RegExp creation outside loops
js-hoist-regexp - - Use loop for min/max instead of sort
js-min-max-loop - - Use Set/Map for O(1) lookups
js-set-map-lookups - - Use toSorted() for immutability
js-tosorted-immutable
- - 通过类或cssText批量处理CSS变更
js-batch-dom-css - - 构建Map以实现重复查找
js-index-maps - - 在循环中缓存对象属性
js-cache-property-access - - 在模块级Map中缓存函数结果
js-cache-function-results - - 缓存localStorage/sessionStorage读取结果
js-cache-storage - - 将多个filter/map合并为单个循环
js-combine-iterations - - 在进行昂贵的比较前先检查数组长度
js-length-check-first - - 提前从函数返回
js-early-exit - - 将RegExp创建提升到循环外部
js-hoist-regexp - - 使用循环而非排序来获取min/max
js-min-max-loop - - 使用Set/Map实现O(1)查找
js-set-map-lookups - - 使用toSorted()实现不可变性
js-tosorted-immutable
8. Advanced Patterns (LOW)
8. 高级模式(低)
- - Store event handlers in refs
advanced-event-handler-refs - - useLatest for stable callback refs
advanced-use-latest
- - 将事件处理程序存储在refs中
advanced-event-handler-refs - - 使用useLatest获取稳定的回调refs
advanced-use-latest
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
rules/async-parallel.md
rules/bundle-barrel-imports.md
rules/_sections.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
阅读单个规则文件以获取详细说明和代码示例:
rules/async-parallel.md
rules/bundle-barrel-imports.md
rules/_sections.md每个规则文件包含:
- 简要说明该规则的重要性
- 错误代码示例及解释
- 正确代码示例及解释
- 额外背景信息与参考资料
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需查看包含所有展开规则的完整指南,请查看:
AGENTS.mdWhen to Use
适用场景
This skill is applicable to execute the workflow or actions described in the overview.
本指南适用于执行概述中描述的工作流或操作。