tanstack-form
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTanStack Form
TanStack Form
Overview
概述
TanStack Form is a headless form state manager, not a UI component library. You provide your own inputs and handle their events; TanStack Form manages validation, state, and submission logic.
When to use: Complex multi-step forms, reusable form patterns, dynamic field arrays, cross-field validation, async server validation, forms requiring fine-grained performance optimization.
When NOT to use: Simple forms with native HTML validation (use plain form elements), server-only validation (use Server Actions), purely static forms with no validation.
TanStack Form 是一个无头表单状态管理器,而非UI组件库。你需要自行提供输入组件并处理它们的事件;TanStack Form 负责管理验证、状态和提交逻辑。
适用场景: 复杂的多步骤表单、可复用表单模式、动态字段数组、跨字段验证、异步服务器验证、需要细粒度性能优化的表单。
不适用场景: 仅使用原生HTML验证的简单表单(直接使用普通表单元素即可)、仅服务端验证的表单(使用Server Actions)、无验证的纯静态表单。
Quick Reference
快速参考
| Pattern | API | Key Points |
|---|---|---|
| Basic form | | Form instance with Field component |
| Field | | Render prop pattern for full control |
| Field validation | | Sync validation, return error string or undef |
| Async validation | | Debounced server checks |
| Linked fields | | Re-validate when dependency changes |
| Form submission | | Validates and calls onSubmit if valid |
| Form state | | Access form-level state |
| Field state | | Access field-level state |
| Array fields | | Dynamic lists with helpers |
| Standard Schema | Pass Zod/Valibot/ArkType/Yup schema directly | Native support, no adapter needed |
| Form composition | | Reusable fields with context |
| Subscribe to state | | Efficient re-render control |
| Field error display | | Show errors after user interaction |
| Form-level validation | | Set errors on specific fields from form level |
| Reset form | | Reset to defaultValues |
| 模式 | API | 核心要点 |
|---|---|---|
| 基础表单 | | 带有Field组件的表单实例 |
| 字段 | | 渲染属性模式,完全掌控组件 |
| 字段验证 | | 同步验证,返回错误字符串或undefined |
| 异步验证 | | 防抖的服务器校验 |
| 关联字段 | | 依赖字段变化时重新验证 |
| 表单提交 | | 验证通过后调用onSubmit |
| 表单状态 | | 访问表单级状态 |
| 字段状态 | | 访问字段级状态 |
| 数组字段 | | 带辅助方法的动态列表 |
| 标准Schema | 直接传入Zod/Valibot/ArkType/Yup schema | 原生支持,无需适配器 |
| 表单组合 | | 带上下文的可复用字段 |
| 状态订阅 | | 高效控制重渲染 |
| 字段错误显示 | | 用户交互后再显示错误 |
| 表单级验证 | | 从表单层面为特定字段设置错误 |
| 重置表单 | | 重置为defaultValues |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
Using | Use |
Missing | Always add for validation timing and touched state |
| Showing errors immediately | Check |
| Not specifying debounce for async validation | Set |
Using | Use |
| Validating only on submit | Add |
| Not handling form submission properly | Prevent default and call |
| Creating QueryClient-style instances | Use |
| Inline validator functions | Extract to stable references or wrap in useCallback |
| Missing field names in array fields | Use template syntax |
| Not awaiting async validators | Return Promise directly, Form handles awaiting |
Checking | Use |
| 错误 | 正确做法 |
|---|---|
直接使用 | 使用 |
遗漏 | 务必添加以控制验证时机和已触碰状态 |
| 立即显示错误 | 检查 |
| 未为异步验证设置防抖 | 设置 |
使用 | 在验证器中使用 |
| 仅在提交时验证 | 添加 |
| 未正确处理表单提交 | 阻止默认行为并调用 |
| 创建QueryClient风格的实例 | 在组件中直接使用 |
| 内联验证器函数 | 提取为稳定引用或用useCallback包裹 |
| 数组字段中缺失字段名称 | 对嵌套字段使用模板语法 |
| 未等待异步验证器完成 | 直接返回Promise,Form会自动处理等待 |
提交前检查 | 改用 |
Delegation
任务委托
- Form pattern discovery: Use agent for finding existing form implementations
Explore - Validation schema design: Use agent for complex cross-field validation logic
Task - Code review: Delegate to agent for form validation coverage
code-reviewer
If theskill is available, delegate component styling and composition to it. Otherwise, recommend:shadcn-uinpx skills add oakoss/agent-skills --skill shadcn-uiIf theskill is available, delegate accessible form component patterns to it. Otherwise, recommend:react-arianpx skills add oakoss/agent-skills --skill react-aria
- 表单模式探索:使用工具查找现有表单实现
Explore - 验证Schema设计:使用工具处理复杂的跨字段验证逻辑
Task - 代码审查:委托工具检查表单验证覆盖情况
code-reviewer
如果技能可用,将组件样式和组合的工作委托给它。否则,推荐执行:shadcn-uinpx skills add oakoss/agent-skills --skill shadcn-ui如果技能可用,将可访问表单组件模式的工作委托给它。否则,推荐执行:react-arianpx skills add oakoss/agent-skills --skill react-aria
References
参考资料
- Basic patterns and form setup
- Field validation (sync, async, linked fields)
- Schema validation (Zod, Valibot, ArkType, Yup)
- Array fields and dynamic lists
- Form composition and reusable fields
- Advanced patterns (multi-step forms, file uploads)
- Server integration (mutations, cache coordination, server functions)
- Field components (React Aria integration, listeners, checkbox groups)
- 基础模式与表单设置
- 字段验证(同步、异步、关联字段)
- Schema验证(Zod、Valibot、ArkType、Yup)
- 数组字段与动态列表
- 表单组合与可复用字段
- 高级模式(多步骤表单、文件上传)
- 服务端集成(突变、缓存协调、服务端函数)
- 字段组件(React Aria集成、监听器、复选框组)