v2.0.0 (2020-12-22)
A new major release!
That's right, a new major release jam packed full of new features and with a side helping of breaking changes.
See the announcement post.
New docs site
Technically not part of this release but definitely worth a visit to check out the new guides!
Event driven validation
Validation is now tailored to specific events (mount
, blur
, submit
, etc).
This provides extra flexibility to tweak validation for specific scenarios - such as triggering async validation only on submit (see example below).
useField({
validate: useCallback(({ trigger, value }) {
// Validation for all events
if (!value) {
throw Error("Value is required.");
}
// Validation for blur only events
if (trigger == "blur" && value.length < 4) {
throw Error("Value must be at least 4 characters.");
}
// Async validation only on submit
if (trigger == "submit") {
return serverSideValidation(value).then((isValid) => {
if (!isValid) {
throw Error("Server side validation failed");
}
});
}
}, [])
})
More info on event driven validation can be found here.
useSubmit hook
There's a new useSubmit
hook which:
- triggers the new
submit
validation event - aggregates field values into a single object
- tracks state of async submission validation
- guards submission logic until validation succeeds
const { isValidating, hasSubmitted, handleSubmit } = useSubmit(() => {
console.log('This is only called if submission validation succeeds!');
});
handleSubmit(); // Trigger submission
More info on submission can be found here.
Breaking changes
initialValue
argument on theuseField
hook is now required (more info)validate
argument on theuseField
hook now receives only a single argument (more info)- removed deprecated properties
touched
andinitalTouched
on theuseField
hook (more info) - removed
initialValid
andinitialError
arguments on theuseField
hook in favor of validation events (more info) - removed
validateOnBlur
,validateOnChange
, andvalidateOnUpdate
arguments on theuseField
hook in favor of validation events (more info) - removed support for returning validation errors as strings without throwing (more info)