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 React from 'react'
30import ReactDOM from 'react-dom/client'
31import { useForm } from '@tanstack/react-form'
32import type { FieldApi } from '@tanstack/react-form'
33β
34function FieldInfo({ field }: { field: FieldApi<any, any> }) {
35 return (
36 <>
37 {field.state.meta.touchedError ? (
38 <em>{field.state.meta.touchedError}</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 (values) => {
52 // Do something with form data
53 console.log(values)
54 },
55 })
56β
57 return (
58 <div>
59 <h1>Simple Form Example</h1>
60 {/* A pre-bound form component */}
61 <form.Provider>
62 <form
63 onSubmit={(e) => {
64 e.preventDefault()
65 e.stopPropagation()
66 void form.handleSubmit()
67 }}
68 >
69 <div>
70 {/* A type-safe and pre-bound field component*/}
71 <form.Field
72 name="firstName"
73 onChange={(value) =>
74 !value
75 ? 'A first name is required'
76 : value.length < 3
77 ? 'First name must be at least 3 characters'
78 : undefined
79 }
80 onChangeAsyncDebounceMs={500}
81 onChangeAsync={async (value) => {
82 await new Promise((resolve) => setTimeout(resolve, 1000))
83 return (
84 value.includes('error') && 'No "error" allowed in first name'
85 )
86 }}
87 children={(field) => {
88 // Avoid hasty abstractions. Render props are great!
89 return (
90 <>
91 <label htmlFor={field.name}>First Name:</label>
92 <input
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 name={field.name}
112 value={field.state.value}
113 onBlur={field.handleBlur}
114 onChange={(e) => field.handleChange(e.target.value)}
115 />
116 <FieldInfo field={field} />
117 </>
118 )}
119 />
120 </div>
121 <form.Subscribe
122 selector={(state) => [state.canSubmit, state.isSubmitting]}
123 children={([canSubmit, isSubmitting]) => (
124 <button type="submit" disabled={!canSubmit}>
125 {isSubmitting ? '...' : 'Submit'}
126 </button>
127 )}
128 />
129 </form>
130 </form.Provider>
131 </div>
132 )
133}
134β
135const rootElement = document.getElementById('root')!
136β
137ReactDOM.createRoot(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/FormApi)