# Changelog ## [7.54.0] - 2024-12-09 ### Changed - `useForm` return methods will be memorized based `formState` update ## [7.53.0] - 2024-8-31 ### Added - add support for `onBlur` with formState `isValid` ### Changed - `validateFields` will only trigger re-render for async validation ## [7.51.0] - 2024-3-2 ### Added - added 'validateFields' to formState ```tsx const { formState: { validateFields }, } = useForm(); ``` ## [7.49.0] - 2023-12-10 ### Added - add reactive `errors` prop at `useForm` ```tsx useForm({ errors, // Server errors }); ``` ## [7.48.0] - 2023-11-05 ### Added - added new `disabled` prop for `useForm` to disable the entire form ``` const App = () => { const [disabled, setDisabled] = useState(false); const { handleSubmit } = useForm({ disabled }); return (
{ setDisabled(true); await sleep(100); setDisabled(false); })} / > ); } ``` ## [7.47.0] - 2023-10-02 ### Added - `reset` api with `keepIsSubmitSuccessful` option, keep successfully submitted form state. ```tsx { reset(formValues, { keepIsSubmitSuccessful: true, }); }} /> ``` ## [7.46.0] - 2023-09-03 ### Added - Controller `disabled` prop ```jsx const [disabled, setDisabled] = useState(false); useController({ disabled, }); ``` - Trigger passed names to construct resolver options - Add `exact` option for array name in `useWatch` ### Changed - Update `isDirty` when setting `disabled` in `register` ## Fixed - Prevent `reset` argument mutation ## [7.45.0] - 2023-06-20 ### Changed - Controller with type check on `onChange` ```diff - onChange: (...event: any[]) => void; + onChange: (event: ChangeEvent | FieldPathValue) => void; ``` - Include missing generic for `useFormContext` ```diff - export const useFormContext: () => UseFormReturn; + export const useFormContext: () => UseFormReturn; ``` ## [7.44.0] - 2023-06-14 ### Added - New `` component ```tsx // Send post request with formData { alert("Great"); }} /> // Send post request with json form data {errors.root?.server.type === 500 && 'Error message'} {errors.root?.server.type === 400 && 'Error message'} // Send post request with formData with fetch
{ await fetch("api", { method: "post", body: formData, }); }} /> ``` - support `TransformedValues` with `useFormContext` `useFormContext()` - added `TTransformedValues` to `FormProvider` FormProviderProps ## [7.43.0] - 2023-01-30 ### Added - support global error type ```tsx const onSubmit = async () => { setError('root.serverError', { type: response.statusCode, }); }; const onClick = () => { setError('root.random', { type: 'random', }); }; return ( <> {errors.root.serverError.type === 400 &&

server response message

}

{errors.root?.random?.message}

); ``` ## [7.42.0] - 2023-01-13 ### Added - build in validation `validate` support second argument for form values ```tsx // Making exported validate function isolated for validation export function validateNumber(_: number, formValus: FormValues) { return formValus.number1 + formValus.number2 === 3; } ; ``` ### Changed - `handleSubmit` no longer catch `onSubmit` callback error - Remove deprecated for `fieldState.invalid` ## [7.41.0] - 2022-12-17 ### Added - `useForm` added `values` props ```tsx const values = await fetch('API'); useForm({ values, // will reset the form when values updates // resetOptions: { // keepDirtyValues: true // } }); ``` - new `isLoading` formState for async `defaultValues` ```tsx const { formState: { isLoading }, } = useForm(); ``` ### Changed - `useForm` support async `defaultValues` props ```tsx const { formState: { isLoading }, } = useForm({ defaultValues: fetch('API'), // resetOptions: { // keepDirtyValues: true // } }); ``` ## [7.40.0] - 2022-11-30 ### Changed - async validation (or combined with sync) will always the take the latest validation result and abort the previous ## [7.39.5] - 2022-11-21 ### Changed - Conditional render `useFormState` will trigger an extra re-render to reflect the current `formState` ## [7.39.0] - 2022-11-2 ### Changed - `isValid` formState is no longer only applicable with `onChange`, `onTouched`, and `onBlur` mode. ## [7.38.0] - 2022-10-19 ### Added - support build-in validation with input type week and time ```tsx ``` ## [7.37.0] - 2022-10-07 ### Added - new formState `defaultValues` ```tsx const { formState, watch } = useForm({ defaultValues: { name: 'test' }, }); const { defaultValues } = useFormState(); const name = watch('name'); return (

Your name was {defaultValues.name}

Updated name is {name}

); ``` ### Changed - defaultValues: complex object data contains prototype methods will not be cloned internally ## [7.36.0] - 2022-9-20 ### Added - reset to support callback syntax ```tsx reset((formValues) => { return { ...formValues, partialData: 'onlyChangeThis', }; }); ``` ## [7.35.0] - 2022-9-10 ### Added - new type `FieldPathByValue` field path by value generic implementation ```tsx function CustomFormComponent< TFieldValues extends FieldValues, Path extends FieldPathByValue, >({ control, name }: { control: Control; name: Path }) { const { field } = useController({ control, name, }); } function App() { const { control } = useForm<{ foo: Date; baz: string; }>(); return ( {/* no error */} {' '} {/* throw an error since baz is string */} ); } ``` ### Changed - form context support children prop type ```tsx
// ✅
// ✅ ``` ## [7.34.0] - 2022-7-28 ### Added - Build in validation support for `useFieldArray` with `rules` prop ```tsx useFieldArray({ name: 'test', rules: { required: true, minLength: 2, maxLength: 10, validate: (fieldArrayValues) => { if (fieldArrayValues[2].title === 'test') { return 'validate Error'; } }, }, }); errors?.test?.root?.message; // access root level errors ``` ## [7.33.0] - 2022-6-24 ## Breaking Change - `@hookform/resolvers` needs to upgraded to version `^2.9.3` above - `useFormContext` do always required to provide a generic type check for your form, without providing generic will now require developers to convert error messages to `String` to pass the type check ```tsx useFormContext(); // ✅ correct usage by provide form type defination const { formState } = useFormContext(); // if generic is missing String(formState.errors?.input?.message); // will need to convert to string ``` ### Changed - Deprecate `NestedValue` and `UnpackNestedValue` type, will be **removed** in the next major version. **Important**: If you are using them, it may cause TS compile error, so please just remove the type usage. ```diff type FormValues = { - select: NestedValue<{ - nested: string - }> + select: { + nested: string + } } type Data = UnpackNestedValue ``` - `formState`'s `errors` is now mapped/merged with `FieldError` - `UseFormHandleSubmit` has removed unused function generic ## [7.32.0] - 2022-6-10 ### Changed - `UseFormRegisterReturn` name type change from `string` to `TFieldName` ## [7.31.0] - 2022-5-11 ### Added - new: `reset` optional prop: `keepDirtyValues` ```tsx reset( { firstName: 'bill', // if firstName is dirty then the value will be retained lastName: 'luo', }, { keepDirtyValues: true }, // keep any changed field ); ``` ### Changed - `useFieldArray` auto-correct field array errors on user action ```tsx const { append } = useFieldArray(); append({ data: '' }); // will auto correct existing field array errors if any ``` ## [7.30.0] - 2022-4-17 ### Changed - improve checkboxes value determine by defaultValues ```tsx useForm({ defaultValues: { checkboxes: [], // register checkbox will be determine as array of checkboxes }, }); register('checkboxes'); // will return array as value ``` ## [7.29.0] - 2022-3-30 ### Changed - tsconfig config change from es2017 to es2018 ## [7.28.0] - 2022-3-13 ### Changed - `register` API options `deps` now support string ```tsx register('test', { deps: 'test' }); ``` ## [7.27.0] - 2022-2-11 ### Added - new option for `setFocus` to select the entire field value ```tsx setFocus('fieldName', { shouldSelect: true }); ``` ## [7.25.2] - 2022-1-29 ### Changed - `onTouched` mode will honor `focusout` event ## [7.25.0] - 2022-1-22 ### Added - `getFieldState` get individual field state ```tsx export default function App() { const { register, getFieldState, formState: { isDirty, isValid }, } = useForm({ mode: 'onChange', defaultValues: { firstName: '', }, }); // you can invoke before render or within the render function const fieldState = getFieldState('firstName'); return (

{getFieldState('firstName').isDirty && 'dirty'}

{getFieldState('firstName').isTouched && 'touched'}

); } ``` ## [7.24.0] - 2022-1-14 ### Changed - `useController` return prop: `onChange`, `onBlur` and `ref` will be memorized with `useCallback` ## [7.23.0] - 2022-1-12 ### Changed - `useFieldArray` change `keyName` is no longer required when field value contains `id` ```tsx const App = () => { const { control, register, handleSubmit } = useForm({ defaultValues: { test: [{ id: 'UUID5678', test: 'data' }], // id value will be retained }, }); const { fields, append } = useFieldArray({ control, name: 'test', }); return (
{fields.map((field, index) => { return ; })}
); }; ``` - `useFormState` will no longer fire state update after hook unmount - `UseFormHandleSubmit` type will infer formValues ## [7.22.0] - 2021-12-14 ### Changed - Browser native reset API will no longer be invoked when `reset` provided with value ```tsx const onSubmit = (data) => {}; React.useEffect(() => { if (formState.isSubmitSuccessful) { reset({ something: '' }); } }, [formState, reset]); handleSubmit(onSubmit); ``` to ```tsx const onSubmit = (data) => { setSubmittedData(data); reset(data); // no longer need to have useEffect }; handleSubmit(onSubmit); ``` ## [7.21.0] - 2021-12-06 ### Changed - `shouldUseNativeValidation` will pass down validation props both at client and server render ```tsx const { register } = useForm() // both client and server render ``` ## [7.20.3] - 2021-11-26 ### Changed - register `onChange` will share same logic with `useController` for non standard event payload ```tsx const { onChange } = register('test'); onChange('stringIsValid'); // this is only valid use case for JS ``` - empty node in `formState` will no longer gets unset ## [7.20.0] - 2021-11-19 ### Added - new `exact` prop for `useWatch` - new `exact` prop for `useFormState` ```tsx useWatch({ name: 'test.test', exact: true, }); useFormState({ name: 'test.test', exact: true, }); ``` ### Changed - `useWatch` subscription will occurred at `useEffect` instead before `render` ## [7.19.0] - 2021-11-05 ### Added - new `resetField` API ```tsx const { resetField } = useForm(); resetField('test'); resetField('test', { keepError: true, keepDirty: true, keepTouched: true, defaultValue: 'test1', }); ``` ### Changed - `useController` will return shallow clone value for the following data type on each rerender - object: `{... value}` - array: `[...value]` ## [7.18.1] - 2021-11-02 - revert `FieldPathWithValue` ## [7.18.0] - 2021-10-28 ### Added - bring back `FieldPathWithValue` - schema errors parent object look up ```tsx const validationSchema = object().shape({ questions: array().min(1, 'Array cannot be empty'), }); // the above schema will be picked up by field array action // the logic applies to group error object eg checkboxes ; ``` ## [7.17.0] - 2021-10-02 ### Added - new type `FieldPathWithValue` to improve generic components type support ```tsx type ExpectedType = { test: string }; const Generic = ({ name, control, }: { name: FieldPathWithValue; control: Control; }) => { const { field: { value, ...fieldProps }, } = useController({ name, control, defaultValue: { test: 'value' }, }); return ; }; ``` ## [7.16.1] - 2021-09-27 ### Changed - `formState` subscription no longer subscribed at `useEffect` instead the execution of each hook ## [7.16.0] - 2021-09-25 ### Added - `register` allowed pass custom `onChange` and `onBlur` ```tsx {}, onBlur: (e) => {}, })} /> ``` ## [7.15.0] - 2021-09-05 ### Added - `useFieldArray` new method `replace()` ```tsx const { control } = useForm({ defaultValues: { test: [{ value: 'lorem' }, { value: 'ipsum' }], }, }); const { fields, replace } = useFieldArray({ control, name: 'test', }); const handleFullyReplacement = (): void => { // remove old and set fully new values replace([{ value: 'dolor' }, { value: 'sit' }, { value: 'amet' }]); }; ``` - Improved to not map types defined with `interface`. ```tsx import { useForm } from 'react-hook-form'; interface CustomValue { custom: string; } type FormValues = { fieldName: CustomValue; }; const { formState: errors } = useForm({ defaultValues: { fieldName: { custom: 'value' } }, }); ``` ## [7.14.0] - 2021-08-27 ### Added - `register` add dependent validation ```tsx const App = () => { const { register, getValues } = useForm(); return (
{ return getValues('lastName') === value; }, })} /> // dependant validation
); }; ``` ## [7.13.0] - 2021-08-22 ### Added `Trigger` - Trigger will enable object name trigger and field array name trigger ```tsx useFieldArray({ name: 'test' }); trigger('name'); // will trigger the whole field array to validate ``` `register` - added a `disabled` prop as an option to toggle input disable attribute - register will be able to seek input DOM reference through the `ref` callback ```tsx register('test', { disabled: true }) // will set value to undefined and pass disabled down to the input attribute
// this input will be registered
``` `useWatch` - added `disabled` prop to toggle the state subscription. ```tsx useWatch({ disabled: true }); // you toggle the subscription ``` `useFormState` - added `disabled` prop to toggle the state subscription. ```tsx useFormState({ disabled: true }); // you toggle the subscription ``` `setValue` - allow set value for non-registered inputs include nested object and array field. ```tsx setValue('test', [{ firstName: 'bill' }, {firstName: 'kotaro}, {firstName: 'joris'}]) // this will works ``` ## [7.12.0] - 2021-07-24 ### Added - new `useForm` config `delayError` ```tsx useForm({ delayError: 500, // delay error appear with 500ms }); ``` ## [7.11.0] - 2021-07-13 ### Added - `update` method to update an field array inputs ```tsx const { update } = useFieldArray(); update(0, data); // update an individual field array node ``` ## [7.10.0] - 2021-07-02 ### Changed - `defaultValue` is no longer a required prop for register input with `useFieldArray` ## [7.9.0] - 2021-06-19 ### Added - new config at `useForm` to enabled native browser validation ```tsx const { register, handleSubmit } = useForm({ shouldUseNativeValidation: true, }); ``` ## [7.8.5] - 2021-06-15 ### Change - `useController` no longer access input `ref` except `focus` event for focus management ## [7.8.0] - 2021-06-5 ### Added - `setValue` support `shouldTouch` to update formState touchFields ```tsx setValue('firstName', 'value', { shouldTouch: true }); ``` - `register` now accept `value` as option ```tsx register('firstName', { value: 'value' }); ``` ### Changed - `isValid` will initialise as `false` ## [7.7.1] - 2021-05-30 ### Fixed - `shouldUnregister: false` should not shallow merge or register absent input fields from `defaultValues` ## [7.7.0] - 2021-05-29 ### Added - `trigger` support focus with error input ```ts trigger('inputName', { shouldFocus: true }); ``` ### Changed - `handleSubmit` will `throw` error within the onSubmit callback ## [7.6.0] - 2021-05-15 ### Changed - `useForm` will `register` missing inputs from `defaultValues` ```tsx const App = () => { const { register, handleSubmit } = useForm({ defaultValues: { test: { firstName: 'bill', lastName: 'luo' }, }, }); const onSubmit = (data) => { // missing registered input will be included console.log(data); // { test: { firstName: 'bill', lastName: 'luo' } } }; return (