Use this file to discover all available pages before exploring further.
This guide will walk you through creating your first form with TanStack Form. We’ll use React as the example framework, but the concepts apply to all supported frameworks.
If you haven’t installed TanStack Form yet, check out the Installation guide first.
Import the useForm hook from the TanStack Form package for your framework:
import { useForm } from '@tanstack/react-form'
2
Create a form instance
Create a form instance with useForm, providing default values and a submit handler:
export default function App() { const form = useForm({ defaultValues: { firstName: '', lastName: '', }, onSubmit: async ({ value }) => { // Do something with form data console.log(value) }, }) // ... rest of component}
3
Create form fields
Use form.Field to create type-safe form fields with validation:
<form onSubmit={(e) => { e.preventDefault() e.stopPropagation() form.handleSubmit() }}> <form.Field name="firstName" validators={{ onChange: ({ value }) => !value ? 'A first name is required' : value.length < 3 ? 'First name must be at least 3 characters' : undefined, }} children={(field) => ( <> <label htmlFor={field.name}>First Name:</label> <input id={field.name} name={field.name} value={field.state.value} onBlur={field.handleBlur} onChange={(e) => field.handleChange(e.target.value)} /> {field.state.meta.isTouched && field.state.meta.errors.length > 0 && ( <em>{field.state.meta.errors.join(', ')}</em> )} </> )} /></form>
4
Add a submit button
Use form.Subscribe to create a submit button that responds to form state: