Outdated guides
Splitting Forms
Large forms can quickly become hard to read when they combine lots of logic with UI. This guide walks through two main ways to organize that code in separate files: extracting field components and splitting a form into sections.
Split field components#
Let's assume you're hooking up an input component:
import { useForm } from '@tanstack/react-form'
function Example() {
const form = useForm({
defaultValues: { firstName: '' },
})
return (
<form.Field name="firstName">
{(field) => (
<>
<label>
First name
<input
type="text"
name={field.name}
value={field.value}
onBlur={field.handleBlur}
onChange={(event) => field.handleChange(event.target.value)}
aria-invalid={field.meta.isInvalid}
/>
</label>
{field.errors.map((error) => (
<small key={error.message} role="alert">
{error.message}
</small>
))}
</>
)}
</form.Field>
)
}Repeating this UI for every field is cumbersome, and we risk forgetting a few props.
The error display doesn't depend on the field's value, so we can reuse it with any form field. Use AnyFieldApi to type its field prop:
import type { AnyFieldApi } from '@tanstack/react-form'
interface FieldInfoProps {
field: AnyFieldApi
}
export function FieldInfo(props: FieldInfoProps) {
return props.field.errors.map((error) => (
<small key={error.message} role="alert">
{error.message}
</small>
))
}The text input element is specific to string fields. Use the FieldWithValue helper type to make sure the component only accepts string fields:
import type { FieldWithValue } from '@tanstack/react-form'
interface TextInputProps {
label: string
field: FieldWithValue<string>
}
export function TextInput(props: TextInputProps) {
const { field, label } = props
return (
<label>
{label}
<input
type="text"
name={field.name}
value={field.value}
onBlur={field.handleBlur}
onChange={(event) => field.handleChange(event.target.value)}
aria-invalid={field.meta.isInvalid}
/>
</label>
)
}Field reactivity
form.Field rerenders automatically when the field's data changes. You may memoize components or use the React Compiler.
See the state subscription section for more details.
Our original form is now much smaller:
import { useForm } from '@tanstack/react-form'
import { TextInput } from './TextInput'
import { FieldInfo } from './FieldInfo'
function Example() {
const form = useForm({
defaultValues: { firstName: '' },
})
return (
<form.Field name="firstName">
{(field) => (
<>
<TextInput field={field} label="First name" />
<FieldInfo field={field} />
</>
)}
</form.Field>
)
}import type { FieldWithValue } from '@tanstack/react-form'
interface TextInputProps {
label: string
field: FieldWithValue<string>
}
export function TextInput(props: TextInputProps) {
const { field, label } = props
return (
<label>
{label}
<input
type="text"
name={field.name}
value={field.value}
onBlur={field.handleBlur}
onChange={(event) => field.handleChange(event.target.value)}
aria-invalid={field.meta.isInvalid}
/>
</label>
)
}import type { AnyFieldApi } from '@tanstack/react-form'
interface FieldInfoProps {
field: AnyFieldApi
}
export function FieldInfo(props: FieldInfoProps) {
return props.field.errors.map((error) => (
<small key={error.message} role="alert">
{error.message}
</small>
))
}Split a large form#
For large forms, you may want to move sections of the UI into separate files. To preserve type safety, each component needs to know which form options to expect:
import { formOptions } from '@tanstack/react-form'
import type { ReactFormType } from '@tanstack/react-form'
export const sharedOptions = formOptions({
// Shared options should include default values
// and validators. Both affect the form's type
defaultValues: { firstName: '' },
validators: [/* ... */],
})
// Get the form's type from these options
export type SharedForm = ReactFormType<typeof sharedOptions>Each section can now accept a form with this type:
import type { SharedForm } from './FormOptions'
interface SplitSectionProps {
form: SharedForm
}
export function SplitSection(props: SplitSectionProps) {
const { form } = props
// You now have typed access to the form's fields
return <form.Field name="firstName">{() => null /* ... */}</form.Field>
}In the main component that defines the form, reuse the shared options:
import { useForm } from '@tanstack/react-form'
import { sharedOptions } from './FormOptions'
function MainForm({
myAsyncData,
}: {
myAsyncData?: typeof sharedOptions.defaultValues
}) {
const form = useForm(sharedOptions)
// You can override the default values with async data
const asyncForm = useForm({
...sharedOptions,
defaultValues: myAsyncData ?? sharedOptions.defaultValues,
})
// You may also add callbacks
const extendedForm = useForm({
...sharedOptions,
onSubmit: () => {
/* ... */
},
onSubmitInvalid: () => {
/* ... */
},
errorVisibility: () => true,
})
}Form reactivity
A form does not rerender on its own.
See the state subscription section for more details.