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 { FieldApi } from "@tanstack/react-form";
33β
34function FieldInfo({ field }: { field: FieldApi<any, any, any, any> }) {
35 return (
36 <>
37 {field.state.meta.touchedErrors ? (
38 <em>{field.state.meta.touchedErrors}</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.Provider>
61 <form
62 onSubmit={(e) => {
63 e.preventDefault();
64 e.stopPropagation();
65 void form.handleSubmit();
66 }}
67 >
68 <div>
69 {/* A type-safe field component*/}
70 <form.Field
71 name="firstName"
72 validators={{
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 onChangeAsyncDebounceMs: 500,
80 onChangeAsync: async ({ value }) => {
81 await new Promise((resolve) => setTimeout(resolve, 1000));
82 return (
83 value.includes("error") &&
84 'No "error" allowed in first name'
85 );
86 },
87 }}
88 children={(field) => {
89 // Avoid hasty abstractions. Render props are great!
90 return (
91 <>
92 <label htmlFor={field.name}>First Name:</label>
93 <input
94 name={field.name}
95 value={field.state.value}
96 onBlur={field.handleBlur}
97 onChange={(e) => field.handleChange(e.target.value)}
98 />
99 <FieldInfo field={field} />
100 </>
101 );
102 }}
103 />
104 </div>
105 <div>
106 <form.Field
107 name="lastName"
108 children={(field) => (
109 <>
110 <label htmlFor={field.name}>Last Name:</label>
111 <input
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 </form.Provider>
132 </div>
133 );
134}
135β
136const rootElement = document.getElementById("root")!;
137β
138createRoot(rootElement).render(<App />);
139```
140β
141## You talked me into it, so what now?
142β
143- Learn TanStack Form at your own pace with our thorough [Walkthrough Guide](../installation) and [API Reference](../reference/FormApi)