react-hook-form-v7-codemod
v0.1.1
Published
jscodeshift codemod for migrating react-hook-form v6 code to react-hook-form-v7 (npm alias for v7).
Downloads
170
Maintainers
Readme
react-hook-form-v7-codemod
Codemod for migrating code from react-hook-form v6 patterns to v7 patterns imported from react-hook-form-v7 (an npm alias package for RHF v7).
Usage
Run against the current directory:
npx react-hook-form-v7-codemodRun against another directory:
npx react-hook-form-v7-codemod --root ../my-appDry-run and print transformed output:
npx react-hook-form-v7-codemod --root ../my-app --dry --printCustom include/exclude globs:
npx react-hook-form-v7-codemod --root ../my-app --include "**/*.{ts,tsx,js,jsx}" --exclude "**/*.test.tsx,**/generated/**"What the codemod changes
- Rewrites
react-hook-formimports/requires toreact-hook-form-v7. - Rewrites deprecated
useTypedControllerimport/calls touseController. - Rewrites v6
errorsdestructuring fromuseForm()/useFormContext()toformState.errors:const { errors } = useForm()->const { formState: { errors } } = useForm()
- Rewrites v6
registerref patterns to v7 spread registration when safely derivable:<input name="x" ref={register} />-><input {...register('x')} /><input name="x" ref={register({ required: true })} />-><input {...register('x', { required: true })} />
- Rewrites custom register object-name calls:
register({ name: 'field' })->register('field')register({ name: 'field', required: true })->register('field', { required: true })
- Rewrites
Controller as={...}usage torender={({ field }) => ...}usage. - Rewrites renamed
useForm/useFormContextAPIs:clearError->clearErrorstriggerValidation->trigger- Handles both destructured methods and member access on
const methods = useForm().
- Rewrites
setErrorv6shouldFocusshape:setError(name, { ..., shouldFocus: true })->setError(name, { ... }, { shouldFocus: true })
- Rewrites
watcharray destructuring:const { a, b } = watch(['a', 'b'])->const [a, b] = watch(['a', 'b'])
- Rewrites
resetoptions rename:reset(values, { isDirty: true })->reset(values, { keepDirty: true })
- Rewrites
touchedtotouchedFieldsonformStateusage. - Rewrites
useFieldArrayfocus boolean args to options objects:append(value, false)->append(value, { shouldFocus: false })prepend(value, true)->prepend(value, { shouldFocus: true })insert(index, value, false)->insert(index, value, { shouldFocus: false })
Best-effort behavior
For patterns that cannot be migrated deterministically, the codemod preserves code and adds an inline marker:
TODO(rhf-v7-codemod): verify migration
Typical ambiguous cases:
- Dynamic or missing field names in ref-based registration patterns.
- Mixed/overlapping destructuring (
errorstogether with existing complexformStateusage). - Dynamic or otherwise non-deterministic register/watch migration shapes.
ControllerandsetErrorpatterns that already include overlapping v7-style arguments/props.
Verification workflow
After codemod execution:
- Run project tests.
- Run type-check (
tsc --noEmitor project equivalent). - Search for
TODO(rhf-v7-codemod): verify migrationand resolve each occurrence manually. - Re-run the codemod to confirm idempotency (no further changes).
