useControlled
This hook allows us to integrate with an existing component (usually a controlled component) or 3rd-party UI library in React Cool Form. With this hook, we can easily to create a reusable controller component to fulfill our needs. See the useControlled Hook to learn more.
note
When working with conditional fields, please ensure the hook is wrapped in a component.
const [fieldProps, meta] = useControlled(name, config);
#
Namestring
The name of the field. We must provide it when using this hook.
#
ConfigAn object
with the following options:
#
formIdstring
The corresponding ID of the useForm
hook. We only need it when using multiple form hooks at the same time.
#
defaultValuestring
The default value of the field. Useful for dealing with the case of conditional fields.
#
validation(value: any, values: FormValues) => any | Promise<any>
A synchronous/asynchronous function that is used for the field-level validation.
#
parse(...args: any[]) => any
A function that takes the event object (or parameters) of the target component's onChange
handler and parses the value of the field that you want to store into the form state. Useful for data type converting.
#
format(value: any) => any
A function that takes the field's value from the form state and formats the value to give to the field. Usually used in conjunction with parse
.
#
errorWithTouchedboolean
Enable/disable the feature of filtering untouched errors, which can help the user focus on typing without being annoyed by the error message. Default is false
.
// Current state: { errors: { foo: "Required" }, touched: {} }// Returns "Required"const [, { error }] = useControlled("foo");
// Current state: { errors: { foo: "Required" }, touched: {} }// Returns undefinedconst [, { error }] = useControlled("foo", { errorWithTouched: true });
// Current state: { errors: { foo: "Required" }, touched: { foo: true } }// Returns "Required"const [, { error }] = useControlled("foo", { errorWithTouched: true });
#
...restPropsAny other props that will be spread into the fieldProps
.
#
Field PropsAn object
with the following properties:
#
namestring
The name of the field.
#
valueany
The value of the field.
#
onChange(...event: any[]) => void
An event handler called when the field's value changed.
#
onBlur(e: React.FocusEvent) => void
An event handler called when the field loses focus.
#
...restPropsAny other props that were passed to the config
.
#
MetaAn object
with the following properties:
#
errorstring | undefined
The current validation error of the field.
#
isTouchedboolean
To indicate whether the field has been touched/visited.
#
isDirtyboolean
To indicate whether the field has been modified.
#
ExampleThe example demonstrates the basic usage of this hook.
note
When using the hook (and not working with field-array), a default value is required.
import { useControlled } from "react-cool-form";
const Field = ({ as, name, defaultValue, validate, parse, format, errorWithTouched, ...restProps}) => { const [fieldProps, { error, isTouched, isDirty }] = useControlled(name, { defaultValue, validate, parse, format, errorWithTouched, ...restProps, }); const Component = as;
return <Component {...fieldProps} />;};