# Initial Values and Resetting

Most TanStack Form guides assume you start with empty values and submit the completed form. Defining default values
for those forms is simple:

```ts fixture=reactHook
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.

## Loading async default values

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:

### Wait for loaded data

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:

<!-- ::start:tabs variant="files" -->

```tsx file="ProfileFormWrapper.tsx" fixture=reactWithHelpers
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} />
}
```

```tsx file="ProfileForm.tsx" fixture=react
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
}
```

<!-- ::end:tabs -->

### Use fallback values

We use empty values as placeholders until the data is ready. The form stays mounted while we update its default values.

```tsx fixture=reactWithHelpers
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
}
```

> [!IMPORTANT] 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.

## Resetting a form

An editable profile usually needs a way to discard any changes. To do this, we can use `form.reset()`:

```tsx fixture=reactForm
<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>
```

## Reset the form after submission

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()`:

```ts fixture=reactWithHelpers
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
}
```

<!-- TODO: Link to query integration guide -->
