Initial values
In most cases you should set initialValues:
setValues handler
With form.setValues you can set all form values. For example, you can set values after you have received a response from the backend API:
setValues partial
form.setValues can also be used to set multiple values at once. The payload will be shallow merged with the current values state:
Initialize form
When called, the form.initialize handler sets initialValues and values to the same value
and marks the form as initialized. It can be used only once. Subsequent form.initialize calls
are ignored.
form.initialize is useful when you want to sync form values with backend API response:
Example with TanStack Query (react-query):
Note that form.initialize will erase all values that were set before it was called.
It is usually a good idea to set readOnly or disabled on all form fields before
form.initialize is called to prevent data loss. You can implement this with
enhanceGetInputProps:
setFieldValue handler
The form.setFieldValue handler allows you to set the value of the field at the given path:
reset handler
The form.reset handler sets values to initialValues and clears all errors:
setInitialValues handler
The form.setInitialValues handler allows you to update initialValues after the form was initialized:
transformValues
Use transformValues to transform values before they get submitted in the onSubmit handler.
For example, it can be used to merge several fields into one or to convert types:
Get transformed values
You can get transformed values outside of the form.onSubmit method by calling form.getTransformedValues.
It accepts values that need to be transformed as an optional argument. If it is not provided, then
the result of form.getValues() transformation will be returned instead:
onValuesChange
The onValuesChange function is called every time form values change. Use it
instead of useEffect to subscribe to form value changes:
form.watch
form.watch is an effect function that allows subscribing to changes of a
specific form field. It accepts a field path and a callback function that is
called with the new value, previous value, touched and dirty field states:
Note that form.watch uses useEffect under the hood – all hook rules apply.
For example, you cannot use form.watch conditionally or inside loops.
form.watch cascade
To loosely subscribe to changes, you can set cascadeUpdates: true.
This allows parent objects to be written to directly, while still having
subscribers to nested keys updated. Additionally, writes to nested keys
will bubble up, triggering parent key subscriptions as well.
{}Get values type
Get transformed values type
To get transformed values (output of transformValues), use the TransformedValues type.
It is useful when you want to create a custom submit function:
Set values type
By default, form value types will be inferred from initialValues.
To avoid that, you can pass a type to the useForm hook. This approach is useful when
types cannot be correctly inferred or when you want to provide more specific types:
Set transformed values type
By default, transformed values type is the same as form values type. To set a different type, you can pass a second generic
argument to the useForm: