Open History to see the last commit
1---
2id: overview
3title: Overview
4---
5β
6TanStack Form is the ultimate solution for handling forms in web applications, providing a powerful and flexible approach to form management. Designed with first-class TypeScript support, headless UI components, and a framework-agnostic design, it streamlines form handling and ensures a seamless experience across various front-end frameworks.
7β
8## Motivation
9β
10Most web frameworks do not offer a comprehensive solution for form handling, leaving developers to create their own custom implementations or rely on less-capable libraries. This often results in a lack of consistency, poor performance, and increased development time. TanStack Form aims to address these challenges by providing an all-in-one solution for managing forms that is both powerful and easy to use.
11β
12With TanStack Form, developers can tackle common form-related challenges such as:
13β
14- Reactive data binding and state management
15- Complex validation and error handling
16- Accessibility and responsive design
17- Internationalization and localization
18- Cross-platform compatibility and custom styling
19β
20By providing a complete solution for these challenges, TanStack Form empowers developers to build robust and user-friendly forms with ease.
21β
22## Enough talk, show me some code already!
23β
24In the example below, you can see TanStack Form in action with the React framework adapter:
25β
26[Open in CodeSandbox](https://codesandbox.io/s/github/tanstack/form/tree/main/examples/react/simple)
27β
28```tsx
29import * as React from 'react'
30import { createRoot } from 'react-dom/client'
31import { useForm } from '@tanstack/react-form'
32import type { AnyFieldApi } from '@tanstack/react-form'
33β
34function FieldInfo({ field }: { field: AnyFieldApi }) {
35 return (
36 <>
37 {field.state.meta.isTouched && !field.state.meta.isValid ? (
38 <em>{field.state.meta.errors.join(', ')}</em>
39 ) : null}
40 {field.state.meta.isValidating ? 'Validating...' : null}
41 </>
42 )
43}
44β
45export default function App() {
46 const form = useForm({
47 defaultValues: {
48 firstName: '',
49 lastName: '',
50 },
51 onSubmit: async ({ value }) => {
52 // Do something with form data
53 console.log(value)
54 },
55 })
56β
57 return (
58 <div>
59 <h1>Simple Form Example</h1>
60 <form
61 onSubmit={(e) => {
62 e.preventDefault()
63 e.stopPropagation()
64 form.handleSubmit()
65 }}
66 >
67 <div>
68 {/* A type-safe field component*/}
69 <form.Field
70 name="firstName"
71 validators={{
72 onChange: ({ value }) =>
73 !value
74 ? 'A first name is required'
75 : value.length < 3
76 ? 'First name must be at least 3 characters'
77 : undefined,
78 onChangeAsyncDebounceMs: 500,
79 onChangeAsync: async ({ value }) => {
80 await new Promise((resolve) => setTimeout(resolve, 1000))
81 return (
82 value.includes('error') && 'No "error" allowed in first name'
83 )
84 },
85 }}
86 children={(field) => {
87 // Avoid hasty abstractions. Render props are great!
88 return (
89 <>
90 <label htmlFor={field.name}>First Name:</label>
91 <input
92 id={field.name}
93 name={field.name}
94 value={field.state.value}
95 onBlur={field.handleBlur}
96 onChange={(e) => field.handleChange(e.target.value)}
97 />
98 <FieldInfo field={field} />
99 </>
100 )
101 }}
102 />
103 </div>
104 <div>
105 <form.Field
106 name="lastName"
107 children={(field) => (
108 <>
109 <label htmlFor={field.name}>Last Name:</label>
110 <input
111 id={field.name}
112 name={field.name}
113 value={field.state.value}
114 onBlur={field.handleBlur}
115 onChange={(e) => field.handleChange(e.target.value)}
116 />
117 <FieldInfo field={field} />
118 </>
119 )}
120 />
121 </div>
122 <form.Subscribe
123 selector={(state) => [state.canSubmit, state.isSubmitting]}
124 children={([canSubmit, isSubmitting]) => (
125 <button type="submit" disabled={!canSubmit}>
126 {isSubmitting ? '...' : 'Submit'}
127 </button>
128 )}
129 />
130 </form>
131 </div>
132 )
133}
134β
135const rootElement = document.getElementById('root')!
136β
137createRoot(rootElement).render(<App />)
138```
139β
140## You talked me into it, so what now?
141β
142- Learn TanStack Form at your own pace with our thorough [Walkthrough Guide](../installation) and [API Reference](../reference/classes/formapi)