Skip to content

createFormControl

Create form state and ready to be subscribed (BETA)

This function create the entire form state subscription and allow you to subscribe update with or without react component. You can use this function without the need of React Context api.

Props


NameTypeDescription
...propsObjectUseFormProps

Returns


NameTypeDescription
formControlObjectcontrol object for useForm hook
controlObjectcontrol object for useController, useFormState, useWatch
...returnsFunctionsuseForm return methods
NOTES
  • This function is published at v7.55.0-next.3 - This function is completely optional, you can consider to use this instead of useFormContext API. - You may find it useful if you would like to subscribe formsState by skipping react re-render.
RULES
  • You should either use this API or context API
const props = createFormControl()
{" "}
<FormProvider {...props} /> // ❌ You don't need provider
<input {...props.register('name')} /> // ✅ Direct use method from createFormControl

Examples:


const { formControl, control, handleSubmit, register } = createFormControl({
mode: 'onChange',
defaultValues: {
firstName: 'Bill'
}
}})
function App() {
useForm({
formControl,
})
return (
<form onSubmit={handleSubmit(data => console.log)}>
<input {...register('name')} />
<FormState />
<Controller />
</form>
);
}
function FormState() {
useFormState({
control // no longer need context api
})
}
function Controller() {
useFormState({
control // no longer need context api
})
}
const { formControl, register } = createFormControl(props)
formControl.subscribe({
formState: {
isDirty: true,
values: true,
},
callback: (formState) => {
if (formState.isDirty) {
// do something here
}
if (formState.values.test.length > 3) {
// do something here
}
},
})
function App() {
const { register } = useForm({
formControl,
})
return (
<form>
<input {...register("test")} />
</form>
)
}

Thank you for your support

If you find React Hook Form to be useful in your project, please consider to star and support it.