useField
Hook to easily manage a single field.
Type#
UseFieldOptions#
Configuration to pass to useField()
form#
The form instance this field should be bound to. If not specified, it will be used the form instance available in FormContext.
format#
A function that takes the value from the form values and formats the value to give to the input (that will be exposed by UseFieldResult.input.value). Common use cases include converting javascript Date values into a localized date string. Almost always used in conjunction with parse.
parse#
A function that takes the value from the input and converts the value into the value you want stored as this field's value in the form. Common usecases include converting strings into Numbers or parsing localized dates into actual javascript Date objects. Almost always used in conjuction with format.
parseOnBlur#
The same as parse but it is called only when the field is blurred.
validate#
A function that receives the value of the field and the values of the form, and it should return a value of type string | string[] | undefined.
stringin case of a single error messagestring[]in case of multiple error messagesundefinedmeans no error
The function can be async.
In case it throws an error, the message of the error will be used as the error of the field.
validateDebounce#
Value that tells if validation debounce should be active and how:
false: debounce will not be active (as per default)true: debounce will be active, with default values forwait(300) andleading(false)- a
number: debounce will be active withwaitas the number you passed, andleadingfalse - a
{ wait?: number; leading?: boolean }object: debounce will be active with the values you passed
validateOnChange#
Whether or not to validate the field every time the field's value changes. false by default.
validateOnBlur#
Whether or not to validate the field when the field is blurred. false by default.
validateOnChangeFields#
Array of field names that should trigger validation of this field whenever they change. If you pass a value that is not undefined, validateOnChange is automatically considered true unless you set it to false.
UseFieldResult#
It is returned by useField() and represents the field instance that gives access to props and methods to easily manage the field.
input#
It's an object of type:
nameis the name of the field.valueis the current value of the field in the form values.onBluris a function that, when called, informs the form instance that this field has been touched.onChangeis a function that, when called, changes the value of the field extrapolating the new value from theReact.ChangeEventevent it receives.
Usually, this input object is spread into the input element props:
name#
The name of the field.
value#
The value of the field.
isTouched#
true if the field has been touched by the user.
error#
The first error of the field, if any.
errors#
The errors of the field, if any.
isValid#
true if the field has no errors.
isDirty#
true if the field has a different value than the initial value.
isValidating#
true if the field validation is running.
form#
The Form instance this field is bound to.
setValue#
Sets the value of the field.
setTouched#
Sets if the field should be considered touched.
setError#
Sets the error of the field.
addError#
Adds an error to the field. It will be merged with the current error of the field.
validate#
Performs field validation. It returns a Promise that is resolved with the errors of the field.