Most TanStack Form guides assume you start with empty values and submit the completed form. Defining default values for those forms is simple:
const form = useForm({
// You may pass a new object on every render.
defaultValues: { name: '' },
})However, forms often start with existing values, and submissions update the backend with the new values. That's where async initial values come in.
Let's assume we're working on a profile form. We're loading the profile data from a query. To use that data in the form, we have two possible approaches:
We don't render the form until our data is ready. Once the data is available, we render our form as usual. To keep it simple, we're using TanStack Query as the example fetching library:
import { useQuery } from '@tanstack/react-query'
import { ProfileForm } from './ProfileForm'
function ProfileFormWrapper() {
const query = useQuery({
queryKey: ['profile'],
queryFn: fetchProfile,
})
if (query.isPending) return <p>Loading...</p>
if (query.isError) return <p>Error!</p>
return <ProfileForm defaultValues={query.data} />
}import { useForm } from '@tanstack/react-form'
interface ProfileFormProps {
defaultValues: {
firstName: string
lastName: string
}
}
export function ProfileForm(props: ProfileFormProps) {
const form = useForm({
defaultValues: props.defaultValues,
})
// ...
return null
}We use empty values as placeholders until the data is ready. The form stays mounted while we update its default values.
import { useForm } from '@tanstack/react-form'
import { useQuery } from '@tanstack/react-query'
const emptyProfile = {
firstName: '',
lastName: '',
}
function ProfileForm() {
const query = useQuery({
queryKey: ['profile'],
queryFn: fetchProfile,
})
const form = useForm({
defaultValues: query.data ?? emptyProfile,
})
// ...
return null
}Updating default values
New default values apply only to fields that have not been touched. If a user has changed or blurred a field, its value is preserved. Remember that a form submission touches all fields.
An editable profile usually needs a way to discard any changes. To do this, we can use form.reset():
<button
type="reset"
onClick={(e) => {
// Since we use controlled inputs,
// the default behaviour can mess with
// things like native select menus.
e.preventDefault()
form.reset()
}}
>
Cancel
</button>Let's say we're sending the edited profile data to an endpoint. In return, we receive the saved values. To optimistically update to new values while our query is updating, we can pass the new form values to form.reset():
import { useForm } from '@tanstack/react-form'
import { useMutation, useQuery } from '@tanstack/react-query'
const emptyProfile = {
firstName: '',
lastName: '',
}
function ProfileForm() {
const query = useQuery({
queryKey: ['profile'],
queryFn: fetchProfile,
})
const saveMutation = useMutation({
mutationFn: saveProfile,
})
const form = useForm({
defaultValues: query.data ?? emptyProfile,
onSubmit: async ({ value, formApi }) => {
const savedValues = await saveMutation.mutateAsync(value)
// While the query is updating, send the expected new values early
formApi.reset(savedValues)
},
})
// ...
return null
}