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' }}