react-hook-form
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReact Hook Form Best Practices
React Hook Form 最佳实践
Comprehensive performance optimization guide for React Hook Form applications. Contains 41 rules across 8 categories, prioritized by impact to guide form development, automated refactoring, and code generation.
这是一份针对React Hook Form应用的全面性能优化指南。包含8个类别共41条规则,按影响优先级排序,可用于指导表单开发、自动化重构和代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new forms with React Hook Form
- Configuring useForm options (mode, defaultValues, validation)
- Subscribing to form values with watch/useWatch
- Integrating controlled UI components (MUI, shadcn, Ant Design)
- Managing dynamic field arrays with useFieldArray
- Reviewing forms for performance issues
在以下场景中可参考这些指南:
- 使用React Hook Form编写新表单
- 配置useForm选项(mode、defaultValues、validation)
- 使用watch/useWatch订阅表单值
- 集成受控UI组件(MUI、shadcn、Ant Design)
- 使用useFieldArray管理动态字段数组
- 检查表单的性能问题
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Form Configuration | CRITICAL | |
| 2 | Field Subscription | CRITICAL | |
| 3 | Controlled Components | HIGH | |
| 4 | Validation Patterns | HIGH | |
| 5 | Field Arrays | MEDIUM-HIGH | |
| 6 | State Management | MEDIUM | |
| 7 | Integration Patterns | MEDIUM | |
| 8 | Advanced Patterns | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 表单配置 | 关键 | |
| 2 | 字段订阅 | 关键 | |
| 3 | 受控组件 | 高 | |
| 4 | 验证模式 | 高 | |
| 5 | 字段数组 | 中高 | |
| 6 | 状态管理 | 中 | |
| 7 | 集成模式 | 中 | |
| 8 | 高级模式 | 低 | |
Quick Reference
快速参考
1. Form Configuration (CRITICAL)
1. 表单配置(关键)
- - Use onSubmit mode for optimal performance
formcfg-validation-mode - - Set reValidateMode to onBlur for post-submit performance
formcfg-revalidate-mode - - Always provide defaultValues for form initialization
formcfg-default-values - - Use async defaultValues for server data
formcfg-async-default-values - - Enable shouldUnregister for dynamic form memory efficiency
formcfg-should-unregister - - Avoid useForm return object in useEffect dependencies
formcfg-useeffect-dependency
- - 使用onSubmit模式以获得最佳性能
formcfg-validation-mode - - 将reValidateMode设置为onBlur以提升提交后性能
formcfg-revalidate-mode - - 始终为表单初始化提供defaultValues
formcfg-default-values - - 为服务端数据使用异步defaultValues
formcfg-async-default-values - - 启用shouldUnregister以提升动态表单的内存效率
formcfg-should-unregister - - 避免在useEffect依赖项中使用useForm返回对象
formcfg-useeffect-dependency
2. Field Subscription (CRITICAL)
2. 字段订阅(关键)
- - Use useWatch instead of watch for isolated re-renders
sub-usewatch-over-watch - - Watch specific fields instead of entire form
sub-watch-specific-fields - - Combine useWatch with getValues for timing safety
sub-usewatch-with-getvalues - - Subscribe deep in component tree where data is needed
sub-deep-subscription - - Avoid calling watch() in render for one-time reads
sub-avoid-watch-in-render - - Provide defaultValue to useWatch for initial render
sub-usewatch-default-value - - Use useFormContext sparingly for deep nesting
sub-useformcontext-sparingly
- - 使用useWatch而非watch以实现独立重渲染
sub-usewatch-over-watch - - 仅监听特定字段而非整个表单
sub-watch-specific-fields - - 结合useWatch与getValues以确保时序安全
sub-usewatch-with-getvalues - - 在组件树中需要数据的深层位置进行订阅
sub-deep-subscription - - 避免在渲染中调用watch()进行一次性读取
sub-avoid-watch-in-render - - 为useWatch提供defaultValues以完成初始渲染
sub-usewatch-default-value - - 谨慎使用useFormContext以处理深层嵌套
sub-useformcontext-sparingly
3. Controlled Components (HIGH)
3. 受控组件(高)
- - Use useController for re-render isolation
ctrl-usecontroller-isolation - - Avoid double registration with useController
ctrl-avoid-double-registration - - Wire Controller field props correctly for UI libraries
ctrl-controller-field-props - - Use single useController per component
ctrl-single-usecontroller-per-component - - Combine local state with useController for UI-only state
ctrl-local-state-combination
- - 使用useController实现重渲染隔离
ctrl-usecontroller-isolation - - 避免使用useController时出现双重注册
ctrl-avoid-double-registration - - 为UI库正确关联Controller字段属性
ctrl-controller-field-props - - 每个组件仅使用一个useController实例
ctrl-single-usecontroller-per-component - - 结合本地状态与useController以处理仅UI相关的状态
ctrl-local-state-combination
4. Validation Patterns (HIGH)
4. 验证模式(高)
- - Define schema outside component for resolver caching
valid-resolver-caching - - Use schema factory for dynamic validation
valid-dynamic-schema-factory - - Access errors via optional chaining or lodash get
valid-error-message-strategy - - Prefer resolver over inline validation for complex rules
valid-inline-vs-resolver - - Use delayError to debounce rapid error display
valid-delay-error - - Consider native validation for simple forms
valid-native-validation
- - 在组件外部定义schema以实现解析器缓存
valid-resolver-caching - - 使用schema工厂实现动态验证
valid-dynamic-schema-factory - - 通过可选链或lodash get访问错误信息
valid-error-message-strategy - - 对于复杂规则,优先使用resolver而非内联验证
valid-inline-vs-resolver - - 使用delayError实现错误提示的防抖
valid-delay-error - - 对于简单表单,可考虑使用原生验证
valid-native-validation
5. Field Arrays (MEDIUM-HIGH)
5. 字段数组(中高)
- - Use field.id as key in useFieldArray maps
array-use-field-id-as-key - - Provide complete default objects for field array operations
array-complete-default-objects - - Separate sequential field array operations
array-separate-crud-operations - - Use single useFieldArray instance per field name
array-unique-fieldarray-per-name - - Use FormProvider for virtualized field arrays
array-virtualization-formprovider
- - 在useFieldArray映射中使用field.id作为key
array-use-field-id-as-key - - 为字段数组操作提供完整的默认对象
array-complete-default-objects - - 拆分连续的字段数组操作
array-separate-crud-operations - - 每个字段名称仅使用一个useFieldArray实例
array-unique-fieldarray-per-name - - 为虚拟化字段数组使用FormProvider
array-virtualization-formprovider
6. State Management (MEDIUM)
6. 状态管理(中)
- - Destructure formState properties before render
formstate-destructure-formstate - - Use useFormState for isolated state subscriptions
formstate-useformstate-isolation - - Use getFieldState for single field state access
formstate-getfieldstate-for-single-field - - Subscribe to specific field names in useFormState
formstate-subscribe-to-specific-fields - - Avoid isValid with onSubmit mode for button state
formstate-avoid-isvalid-with-onsubmit
- - 在渲染前解构formState属性
formstate-destructure-formstate - - 使用useFormState实现独立状态订阅
formstate-useformstate-isolation - - 使用getFieldState访问单个字段的状态
formstate-getfieldstate-for-single-field - - 在useFormState中订阅特定字段名称
formstate-subscribe-to-specific-fields - - 在onSubmit模式下避免使用isValid控制按钮状态
formstate-avoid-isvalid-with-onsubmit
7. Integration Patterns (MEDIUM)
7. 集成模式(中)
- - Verify shadcn Form component import source
integ-shadcn-form-import - - Wire shadcn Select with onValueChange instead of spread
integ-shadcn-select-wiring - - Use Controller for Material-UI components
integ-mui-controller-pattern - - Transform values at Controller level for type coercion
integ-value-transform
- - 验证shadcn Form组件的导入源
integ-shadcn-form-import - - 使用onValueChange而非展开操作关联shadcn Select
integ-shadcn-select-wiring - - 为Material-UI组件使用Controller
integ-mui-controller-pattern - - 在Controller层转换值以实现类型转换
integ-value-transform
8. Advanced Patterns (LOW)
8. 高级模式(低)
- - Wrap FormProvider children with React.memo
adv-formprovider-memo - - Disable DevTools in production and during performance testing
adv-devtools-performance - - Create test wrapper with QueryClient and AuthProvider
adv-testing-wrapper
- - 使用React.memo包裹FormProvider的子组件
adv-formprovider-memo - - 在生产环境和性能测试期间禁用DevTools
adv-devtools-performance - - 创建包含QueryClient和AuthProvider的测试包装器
adv-testing-wrapper
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
- Reference files:
references/{prefix}-{slug}.md
阅读单个参考文件以获取详细说明和代码示例:
- 章节定义 - 类别结构和影响级别
- 规则模板 - 添加新规则的模板
- 参考文件:
references/{prefix}-{slug}.md
Related Skills
相关技能
- For schema validation with Zod resolver, see skill
zod - For React 19 server actions, see skill
react-19 - For UI/UX form design, see skill
frontend-design
- 如需使用Zod解析器进行schema验证,请查看技能
zod - 如需React 19服务端操作,请查看技能
react-19 - 如需UI/UX表单设计,请查看技能
frontend-design
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需查看包含所有扩展规则的完整指南:
AGENTS.md