FieldApi
The FieldApi
provides helpers for interacting with a specific field.
FieldApi
methods
getInputProps
Returns props that can be spread onto native form controls or thin wrappers around them. It's generally recommended to use this with native form controls. And pass any other props through this helper.
It's important that the component you spread the props into accepts the ref
prop.
This allows RVF to set the value of the field when setValue is called, and is used
to focus the field when it has an error.
<input {...field.getInputProps({ type: "number" })} />
getControlProps
Returns props that can be spread into controlled components to use as a field.
It's important to pass the provided ref
to a focusable HTML element.
This allows the field to be focused when it has an error and also disables RVF's default
behavior of automatically listening to changes in the field.
getHiddenInputProps
Returns props that can be spread into a native form control to use as a hidden field.
This is useful in combination with getControlProps
.
name
Returns the name of the field.
value
Returns the curent value of the field.
setValue
Sets the value of the field.
defaultValue
Returns the default value of the field.
touched
Returns whether or not the field has been touched.
setTouched
Manually sets the touched state of the field.
dirty
Returns whether or not the field has been changed.
setDirty
Manually sets the dirty state of the field.
error
Returns the error message for the field, if there is one.
clearError
Clears the error message for the field.
validate
Manually triggers validation for the field.
reset
Resets the field to its initial value.
This also resets any touched, dirty, or validation errors for the field.
This works for both controlled and uncontrolled fields.
For uncontrolled fields, this will manually set the value of the form control using the ref
returned by field
.
onChange
Has the same behavior as getControlProps().onChange
.
Can be useful when simply spreading getControlProps
isn't enough for your use-case.
onBlur
Has the same behavior as getControlProps().onBlur
.
Can be useful when simply spreading getControlProps
isn't enough for your use-case.
refs
You usually don't need to use this. It's intended for advanced use-cases.
Allows you to directly set the ref
of the field.
There are two functions available: refs.controlled
and refs.transient
.
The ref you choose to use will change the way RVF interacts with the field.
If you use refs.transient
, then RVF will treat the field as an uncontrolled field.
If you use refs.controlled
, then RVF will treat the field as a controlled field.