Usage
The use-field hook is a simpler alternative to use-form. It can be used to
manage the state of a single input without the need to create a form:
use-field API
The use-field hook accepts the following options object as a single argument:
And returns the following object:
Validate on blur
To validate the field on blur, set the validateOnBlur option to true:
Validate on change
To validate the field on change, set the validateOnChange option to true:
Async validation
The validate option accepts both async and sync functions. In both cases, the function
must return an error message that will be displayed to the user or null if the value
is valid. To keep track of async validation state, use the isValidating property:
Async validation can be used with the validateOnBlur option, but it's not recommended with
validateOnChange because it will trigger validation on every key press, which may
lead to race conditions:
Touched and dirty
To get information about whether the field has been focused at least once, use the isTouched method.
To check if the value has been changed from the initial value, use the isDirty method:
Dirty: not dirty
Touched: not touched
Clear error on change
By default, the error message is cleared when the value changes. To disable this behavior,
set the clearErrorOnChange option to false:
Uncontrolled mode
The uncontrolled mode of the use-field hook works similarly to the uncontrolled mode of use-form.
In uncontrolled mode, rerenders are minimized and the input value is managed by the input itself.
It is useful if you experience performance issues with controlled mode, but in most cases controlled
mode is recommended as it always provides up-to-date field information as React state.