- new formState
isSubmitSuccessful
to indicate successful submission setError
now support focus on the actual input
setError('test', { message: 'This is required', shouldFocus: true });
- with
shouldUnregister:false
defaultValues
data will be part of the submission data - with
shouldUnregister:false
conditional field is going to work withuseFieldArray
setValue
now supportuseFieldArray
- setValue('test', 'data')
+ setValue('test', [{ test: '123' }]) // make it work for useFieldArray and target a field array key
- remove
exact
config at clearErrors
- clearErrors('test', { exact: false })
+ clearErrors('test') // does it automatically in the lib
clearError
have second option argument for clear errors which are exact or key name
register('test.firstName', { required: true });
register('test.lastName', { required: true });
clearErrors('test', { exact: false }); // will clear both errors from test.firstName and test.lastName
clearErrors('test.firstName'); // for clear single input error
- all types from this lib has been exported. Important: only documented type: https://react-hook-form.com/ts will avoid breaking change.
errors
is also part offormState
objectdisabled
input will not be part of the submission data by following the HTML standard
Controller
'srender
prop will pass downname
prophandleSubmit
take a second callback for errors callback- new mode
onTouched
will only trigger validation after inputs are touched
register
no longer compareref
difference with React Native
- IE 11 version will be required to install
@babel/runtime-corejs3
as dependency at your own project
- revert
getValues
will return default values before inputs registration
resolver
supports both async and syncgetValues
will return default values before inputs registration
- export
ArrayField
type
- error message will support array of messages for specific type
- export type ValidateResult = Message | boolean | undefined;
+ export type ValidateResult = Message | Message[] | boolean | undefined;
- Controller
onFocus
works with React Native - Controller stop producing
checked
prop by booleanvalue
- export
UseFormOptions
,UseFieldArrayOptions
,FieldError
,Field
andMode
type
- export
ValidationRules
type
- config for
shouldUnregister
which allow input to be persist even after unmount
useForm({
shouldUnregister: false, // unmount input state will be remained
});
- auto focus with useFieldArray
append({}, (autoFocus = true));
prepend({}, (autoFocus = true));
insert({}, (autoFocus = true));
- TS: NestedValue
import { useForm, NestedValue } from 'react-hook-form';
type FormValues = {
key1: string;
key2: number;
key3: NestedValue<{
key1: string;
key2: number;
}>;
key4: NestedValue<string[]>;
};
const { errors } = useForm<FormValues>();
errors?.key1?.message; // no type error
errors?.key2?.message; // no type error
errors?.key3?.message; // no type error
errors?.key4?.message; // no type error
useWatch
(new) subscribe to registered inputs.
<input name="test" ref={register} />;
function IsolateReRender() {
const { state } = useWatch({
name: 'test',
control,
defaultValue: 'default',
});
return <div>{state}</div>;
}
getValues()
support array of field names
getValues(['test', 'test1']); // { test: 'test', test1: 'test1' }
useForm({ mode: 'all' })
support all validation
-
rename
validationResolver
toresolver
-
rename
validationContext
tocontext
-
rename
validateCriteriaMode
tocriteriaMode
-
rename
triggerValidation
totrigger
-
rename
clearError
toclearErrors
-
rename
FormContext
toFormProvider
-
rename
dirty
toisDirty
-
dirtyFields
change type fromSet
toObject
-
Controller with render props API, and removed the following props:
- onChange
- onChangeName
- onBlur
- onBlurName
- valueName
-<Controller
- as={CustomInput}
- valueName="textValue"
- onChangeName="onTextChange"
- control={control}
- name="test"
-/>
+<Controller
+ render={({ onChange, onBlur, value }) => (
+ <CustomInput onTextChange={onChange} onBlur={onBlur} textValue={value} />
+ )}
+ control={control}
+ name="test"
+/>
setError
will focus one error at a time and remove confusing set multiple errors, behavior change.- setError will persis an error if it's not part of the form, which requires manual remove with clearError
- setError error will be removed by validation rules, rules always take over errors
- setError('test', 'test', 'test')
+ setError('test', { type: 'test', message: 'bill'})
setValue
will focus on input at a time
setValue('test', 'value', { shouldValidate: false, shouldDirty: false })
- remove
validationSchema
and embrace validationresolver
- remove
nest
option forwatch
&getValues
, so data return from both methods will be in FormValues shape.
-getValues({ nest: true }); // { test: { data: 'test' }}
-watch({ nest: true }); // { test: { data: 'test' }}
+getValues(); // { test: { data: 'test' }}
+watch(); // { test: { data: 'test' }}