react-principle-engineer
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReact Core Team React Principles Best Practices
React核心团队推荐的React开发原则与最佳实践
Comprehensive React development principles extracted from the official React documentation. Contains 52 rules across 8 categories prioritized by impact on code quality and maintainability.
本文整理自React官方文档中的全面开发原则,包含8个分类下的52条规则,按对代码质量和可维护性的影响优先级排序。
When to Apply
适用场景
Reference these guidelines when:
- Writing React components with hooks
- Managing component and application state
- Synchronizing with external systems (APIs, browser APIs)
- Debugging unexpected re-renders or stale state
- Structuring state for complex UIs
- Deciding between state, refs, context, and reducers
在以下场景中可参考这些指南:
- 使用Hooks编写React组件时
- 管理组件与应用状态时
- 与外部系统(API、浏览器API)同步时
- 调试意外重渲染或过期状态时
- 为复杂UI设计状态结构时
- 在state、refs、context和reducers之间做选择时
Rule Categories by Priority
按优先级排序的规则分类
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Component Purity | HIGH | |
| 2 | State Structure | HIGH | |
| 3 | State Sharing | HIGH | |
| 4 | Effect Patterns | HIGH | |
| 5 | Refs Usage | MEDIUM | |
| 6 | Reducer Patterns | MEDIUM | |
| 7 | Context Patterns | MEDIUM | |
| 8 | Event Handling | MEDIUM | |
| 优先级 | 分类 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 组件纯净性 | 高 | |
| 2 | 状态结构 | 高 | |
| 3 | 状态共享 | 高 | |
| 4 | Effect模式 | 高 | |
| 5 | Refs用法 | 中 | |
| 6 | Reducer模式 | 中 | |
| 7 | Context模式 | 中 | |
| 8 | 事件处理 | 中 | |
Quick Reference
快速参考
1. Component Purity (HIGH)
1. 组件纯净性(高优先级)
- - Never mutate external variables during render
pure-no-external-mutations - - Same props/state always produce same JSX
pure-same-inputs-same-outputs - - Local mutation during render is fine
pure-local-mutation-allowed - - Use StrictMode to catch purity violations
pure-strict-mode-detection - - Put side effects in event handlers
pure-side-effects-in-handlers - - Treat props as immutable snapshots
pure-props-as-readonly - - Render order shouldn't matter
pure-render-independence - - Effects are escape hatches, not primary pattern
pure-use-effect-last-resort - - Pure components enable React's features
pure-why-purity-matters
- - 渲染期间绝不要修改外部变量
pure-no-external-mutations - - 相同的props/state必须生成相同的JSX
pure-same-inputs-same-outputs - - 渲染期间的本地修改是允许的
pure-local-mutation-allowed - - 使用StrictMode检测纯净性违规问题
pure-strict-mode-detection - - 将副作用放在事件处理函数中
pure-side-effects-in-handlers - - 将props视为不可变快照
pure-props-as-readonly - - 渲染顺序不应影响结果
pure-render-independence - - Effects是兜底方案,而非主要模式
pure-use-effect-last-resort - - 纯净组件是React特性的基础
pure-why-purity-matters
2. State Structure (HIGH)
2. 状态结构(高优先级)
- - Group related state variables together
state-group-related - - Avoid contradictory state (use enums)
state-avoid-contradictions - - Don't store values that can be derived
state-avoid-redundant - - Store IDs, not duplicate objects
state-avoid-duplication - - Flatten deeply nested state
state-flatten-nested - - Don't initialize state from props
state-no-mirror-props - - Always create new objects/arrays
state-immutable-updates - - State is a snapshot at render time
state-snapshot-behavior - - Use updaters for sequential updates
state-updater-functions - - Use key to reset component state
state-keys-reset
- - 将相关的状态变量分组
state-group-related - - 避免矛盾状态(使用枚举)
state-avoid-contradictions - - 不要存储可推导的值
state-avoid-redundant - - 存储ID,而非重复对象
state-avoid-duplication - - 扁平化深层嵌套的状态
state-flatten-nested - - 不要从props初始化状态
state-no-mirror-props - - 始终创建新的对象/数组
state-immutable-updates - - 状态是渲染时的快照
state-snapshot-behavior - - 对于连续更新使用更新器函数
state-updater-functions - - 使用key重置组件状态
state-keys-reset
3. State Sharing (HIGH)
3. 状态共享(高优先级)
- - Lift state to nearest common ancestor
share-lift-state-up - - One source of truth for each piece
share-single-source-truth - - Controlled vs uncontrolled patterns
share-controlled-uncontrolled - - Props flow down, events bubble up
share-props-down-events-up - - Use composition to avoid drilling
share-composition-over-drilling - - Component identity affects state
share-preserve-reset-identity
- - 将状态提升到最近的共同祖先组件
share-lift-state-up - - 每个数据片段只有单一数据源
share-single-source-truth - - 受控与非受控组件模式
share-controlled-uncontrolled - - Props向下传递,事件向上冒泡
share-props-down-events-up - - 使用组合避免props透传
share-composition-over-drilling - - 组件标识会影响状态
share-preserve-reset-identity
4. Effect Patterns (HIGH)
4. Effect模式(高优先级)
- - Effects sync with external systems
effect-synchronization - - Always provide cleanup functions
effect-cleanup - - Include all dependencies in array
effect-dependencies - - Separate independent synchronizations
effect-separate-concerns - - Think sync/unsync, not mount/unmount
effect-think-sync-not-lifecycle - - Don't use effects for derived state
effect-not-for-derived-state - - Don't use effects for event responses
effect-not-for-events - - Proper patterns for data fetching
effect-data-fetching - - Never suppress dependency warnings
effect-never-suppress-linter - - Remove effects that don't need to be effects
effect-remove-unnecessary
- - Effects用于与外部系统同步
effect-synchronization - - 始终提供清理函数
effect-cleanup - - 依赖数组中包含所有相关依赖
effect-dependencies - - 分离独立的同步逻辑
effect-separate-concerns - - 思考同步/取消同步,而非挂载/卸载
effect-think-sync-not-lifecycle - - 不要用Effects处理派生状态
effect-not-for-derived-state - - 不要用Effects响应事件
effect-not-for-events - - 数据获取的正确模式
effect-data-fetching - - 绝不要禁用依赖项警告
effect-never-suppress-linter - - 移除不需要作为Effects的逻辑
effect-remove-unnecessary
5. Refs Usage (MEDIUM)
5. Refs用法(中优先级)
- - Refs are escape hatches from React
ref-escape-hatch - - Don't read/write refs during render
ref-no-render-access - - Use refs for DOM manipulation
ref-dom-manipulation - - Refs for mutable values like timers
ref-mutable-values - - When to use refs vs state
ref-best-practices
- - Refs是React的兜底方案
ref-escape-hatch - - 不要在渲染期间读写refs
ref-no-render-access - - 使用refs操作DOM
ref-dom-manipulation - - Refs用于存储计时器等可变值
ref-mutable-values - - 何时使用refs而非state
ref-best-practices
6. Reducer Patterns (MEDIUM)
6. Reducer模式(中优先级)
- - When to use reducers over useState
reducer-when-to-use - - Actions describe what happened
reducer-actions - - Reducers must be pure
reducer-pure-functions - - Standard reducer structure patterns
reducer-structure - - Extract reducers to separate files
reducer-extract-from-component
- - 何时用reducers替代useState
reducer-when-to-use - - 动作描述发生的事件
reducer-actions - - Reducers必须是纯函数
reducer-pure-functions - - 标准的reducer结构模式
reducer-structure - - 将reducers提取到单独文件
reducer-extract-from-component
7. Context Patterns (MEDIUM)
7. Context模式(中优先级)
- - When to use context vs props
context-when-to-use - - Create, use, provide pattern
context-create-use-provide - - Combine context with reducers
context-with-reducer - - Meaningful default values
context-default-values
- - 何时用context而非props
context-when-to-use - - 创建、使用、提供的模式
context-create-use-provide - - 将context与reducers结合使用
context-with-reducer - - 有意义的默认值
context-default-values
8. Event Handling (MEDIUM)
8. 事件处理(中优先级)
- - Pass handlers, don't call them inline
event-pass-handlers - - Side effects belong in handlers
event-side-effects - - Event propagation and stopPropagation
event-propagation - - Handler naming conventions (handle/on)
event-naming
- - 传递处理函数,不要内联调用
event-pass-handlers - - 副作用属于处理函数
event-side-effects - - 事件冒泡与stopPropagation
event-propagation - - 处理函数命名规范(handle/on前缀)
event-naming
How to Use
使用方式
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Example rules: pure-no-external-mutations, effect-synchronization
阅读单个参考文件获取详细说明和代码示例:
- 章节定义 - 分类结构与影响级别
- 示例规则:pure-no-external-mutations, effect-synchronization
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded: AGENTS.md
包含所有规则扩展说明的完整指南:AGENTS.md