react-code-smells
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePrincipal Engineering React Best Practices
高级工程师级React最佳实践
Principal-engineer-level refactoring guide for React applications. Contains 42 rules across 8 categories, prioritized by impact from critical (state architecture, composition) to incremental (testability). Each rule includes code smell indicators, before/after examples, and safe transformation steps.
面向React应用的高级工程师级重构指南。包含8个分类下的42条规则,按影响优先级排序,从关键(状态架构、组件组合)到增量(可测试性)。每条规则都包含代码异味指标、重构前后示例以及安全的转换步骤。
When to Apply
适用场景
Reference these guidelines when:
- Refactoring existing React codebases
- Reviewing PRs for architectural issues
- Identifying technical debt
- Planning large-scale refactoring efforts
- Deciding whether to extract, inline, or restructure components
- Improving testability of React code
在以下场景中参考本指南:
- 重构现有React代码库
- 审查PR中的架构问题
- 识别技术债务
- 规划大规模重构工作
- 决定是否提取、内联或重组组件
- 提升React代码的可测试性
Rule Categories by Priority
按优先级划分的规则分类
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | State Architecture | CRITICAL | |
| 2 | Component Composition | CRITICAL | |
| 3 | Abstraction Quality | HIGH | |
| 4 | Coupling & Cohesion | HIGH | |
| 5 | Hook Hygiene | MEDIUM-HIGH | |
| 6 | Render Patterns | MEDIUM | |
| 7 | Side Effect Management | MEDIUM | |
| 8 | Testability | LOW-MEDIUM | |
| 优先级 | 分类 | 影响级别 | 前缀 |
|---|---|---|---|
| 1 | 状态架构 | 关键 | |
| 2 | 组件组合 | 关键 | |
| 3 | 抽象质量 | 高 | |
| 4 | 耦合与内聚 | 高 | |
| 5 | Hook规范 | 中高 | |
| 6 | 渲染模式 | 中 | |
| 7 | 副作用管理 | 中 | |
| 8 | 可测试性 | 中低 | |
Quick Reference
快速参考
1. State Architecture (CRITICAL)
1. 状态架构(关键)
- - Replace prop drilling with compound components
state-prop-drilling-compound - - Replace prop drilling with component composition
state-prop-drilling-composition - - Replace synchronized state with derived calculations
state-derived-calculation - - Replace boolean explosion with state machines
state-machine-complex - - Colocate state with components that use it
state-colocation - - Consolidate related useState calls into useReducer
state-reducer-consolidation - - Use context selectors to prevent cascade re-renders
state-context-selector - - Move shareable state to URL parameters
state-url-sync
- - 用复合组件替代属性透传
state-prop-drilling-compound - - 用组件组合替代属性透传
state-prop-drilling-composition - - 用派生计算替代同步状态
state-derived-calculation - - 用状态机替代布尔值膨胀
state-machine-complex - - 将状态与使用它的组件放在一起
state-colocation - - 将相关的useState调用合并为useReducer
state-reducer-consolidation - - 使用Context选择器防止级联重渲染
state-context-selector - - 将可共享状态移至URL参数
state-url-sync
2. Component Composition (CRITICAL)
2. 组件组合(关键)
- - Extract shared logic with render props or hooks
comp-render-props-extraction - - Use slots/children instead of configuration props
comp-slots-over-props - - Split god components by responsibility
comp-god-component-split - - Choose controlled vs uncontrolled deliberately
comp-controlled-uncontrolled - - Extract headless components for reusable behavior
comp-headless-extraction - - Avoid untyped props spreading
comp-props-spreading - - Use polymorphic 'as' prop for flexible elements
comp-polymorphic-as - - Validate compound component children
comp-children-validation
- - 用渲染属性或Hooks提取共享逻辑
comp-render-props-extraction - - 使用插槽/子组件替代配置属性
comp-slots-over-props - - 按职责拆分巨型组件
comp-god-component-split - - 谨慎选择受控与非受控组件
comp-controlled-uncontrolled - - 提取无头组件以实现可复用行为
comp-headless-extraction - - 避免无类型的属性展开
comp-props-spreading - - 使用多态
comp-polymorphic-as属性实现灵活元素as - - 验证复合组件的子组件
comp-children-validation
3. Abstraction Quality (HIGH)
3. 抽象质量(高)
- - Resist premature abstraction (Rule of Three)
abs-premature-abstraction - - Prefer composition over configuration
abs-over-configuration - - Extract custom hooks for reusable logic
abs-extract-custom-hook - - Know when to inline vs extract components
abs-inline-vs-extract - - Fix wrong abstractions by inlining first
abs-wrong-abstraction-fix - - Separate utility hooks from domain hooks
abs-utility-vs-domain
- - 避免过早抽象(三次原则)
abs-premature-abstraction - - 优先选择组合而非配置
abs-over-configuration - - 提取自定义Hook以实现可复用逻辑
abs-extract-custom-hook - - 了解何时内联或提取组件
abs-inline-vs-extract - - 先通过内联修复错误的抽象
abs-wrong-abstraction-fix - - 区分工具Hook与领域Hook
abs-utility-vs-domain
4. Coupling & Cohesion (HIGH)
4. 耦合与内聚(高)
- - Colocate related files by feature
couple-colocation-files - - Use dependency injection for external services
couple-dependency-injection - - Apply interface segregation to props
couple-interface-segregation - - Break circular dependencies with inversion
couple-circular-deps - - Import from stable public APIs only
couple-stable-imports
- - 按功能将相关文件放在一起
couple-colocation-files - - 对外部服务使用依赖注入
couple-dependency-injection - - 对属性应用接口隔离原则
couple-interface-segregation - - 通过反转打破循环依赖
couple-circular-deps - - 仅从稳定的公共API导入
couple-stable-imports
5. Hook Hygiene (MEDIUM-HIGH)
5. Hook规范(中高)
- - Ensure hook dependencies are stable
hook-dependency-stability - - Compose small hooks rather than one large hook
hook-composition-over-size - - Choose hook return type by use case
hook-return-object-vs-tuple - - Never call hooks conditionally
hook-conditional-usage - - Follow hook naming conventions
hook-naming-conventions
- - 确保Hook依赖稳定
hook-dependency-stability - - 组合小型Hook而非单个大型Hook
hook-composition-over-size - - 根据用例选择Hook返回类型
hook-return-object-vs-tuple - - 绝不要有条件地调用Hook
hook-conditional-usage - - 遵循Hook命名规范
hook-naming-conventions
6. Render Patterns (MEDIUM)
6. 渲染模式(中)
- - Use early returns for conditional rendering
render-conditional-early-return - - Use stable, unique keys for lists
render-list-key-stability - - Avoid inline objects in JSX props
render-avoid-inline-objects - - Use fragments to avoid wrapper divs
render-fragment-cleanup
- - 使用提前返回处理条件渲染
render-conditional-early-return - - 对列表使用稳定且唯一的Key
render-list-key-stability - - 避免在JSX属性中使用内联对象
render-avoid-inline-objects - - 使用Fragment避免额外的包裹Div
render-fragment-cleanup
7. Side Effect Management (MEDIUM)
7. 副作用管理(中)
- - Move event responses from effects to handlers
effect-to-event-handler - - Always clean up effect side effects
effect-cleanup-required - - Separate unrelated effects
effect-single-responsibility - - Remove effects that aren't synchronization
effect-avoid-unnecessary
- - 将事件响应从Effect移至事件处理器
effect-to-event-handler - - 始终清理Effect的副作用
effect-cleanup-required - - 分离不相关的Effect
effect-single-responsibility - - 移除非同步用途的Effect
effect-avoid-unnecessary
8. Testability (LOW-MEDIUM)
8. 可测试性(中低)
- - Create seams for testable components
test-seam-creation - - Test behavior, not implementation details
test-behavior-over-implementation - - Extract logic to hooks for testability
test-extract-for-testability - - Mock at boundaries, not internal details
test-mock-boundaries
- - 为可测试组件创建接缝
test-seam-creation - - 测试行为而非实现细节
test-behavior-over-implementation - - 将逻辑提取到Hook以提升可测试性
test-extract-for-testability - - 在边界处Mock而非内部细节
test-mock-boundaries
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明和代码示例:
- 章节定义 - 分类结构和影响级别
- 规则模板 - 添加新规则的模板
Related Skills
相关技能
- For React 19 API best practices, see skill
react - For React core principles, see skill
react-principle-engineer - For form handling, see skill
react-hook-form
- 关于React 19 API最佳实践,请查看技能
react - 关于React核心原则,请查看技能
react-principle-engineer - 关于表单处理,请查看技能
react-hook-form
References
参考资料
- react.dev - Official React documentation
- kentcdodds.com - Advanced React patterns
- patterns.dev - Design patterns in JavaScript
- testing-library.com - Testing best practices
- react.dev - React官方文档
- kentcdodds.com - 高级React模式
- patterns.dev - JavaScript设计模式
- testing-library.com - 测试最佳实践