MCPcopy Create free account
hub / github.com/react-hook-form/react-hook-form / useForm

Function useForm

src/useForm.ts:44–190  ·  view source on GitHub ↗
(
  props: UseFormProps<TFieldValues, TContext, TTransformedValues> = {},
)

Source from the content-addressed store, hash-verified

42 * ```
43 */
44export function useForm<
45 TFieldValues extends FieldValues = FieldValues,
46 TContext = any,
47 TTransformedValues = TFieldValues,
48>(
49 props: UseFormProps<TFieldValues, TContext, TTransformedValues> = {},
50): UseFormReturn<TFieldValues, TContext, TTransformedValues> {
51 const _formControl = React.useRef<
52 UseFormReturn<TFieldValues, TContext, TTransformedValues> | undefined
53 >(undefined);
54 const _values = React.useRef<typeof props.values>(undefined);
55 const [formState, updateFormState] = React.useState<FormState<TFieldValues>>({
56 isDirty: false,
57 isValidating: false,
58 isLoading: isFunction(props.defaultValues),
59 isSubmitted: false,
60 isSubmitting: false,
61 isSubmitSuccessful: false,
62 isValid: false,
63 submitCount: 0,
64 dirtyFields: {},
65 touchedFields: {},
66 validatingFields: {},
67 errors: props.errors || {},
68 disabled: props.disabled || false,
69 isReady: false,
70 defaultValues: isFunction(props.defaultValues)
71 ? undefined
72 : props.defaultValues,
73 });
74
75 if (!_formControl.current) {
76 if (props.formControl) {
77 _formControl.current = {
78 ...props.formControl,
79 formState,
80 };
81
82 if (props.defaultValues && !isFunction(props.defaultValues)) {
83 props.formControl.reset(props.defaultValues, props.resetOptions);
84 }
85 } else {
86 const { formControl, ...rest } = createFormControl(props);
87
88 _formControl.current = {
89 ...rest,
90 formState,
91 };
92 }
93 }
94
95 const control = _formControl.current.control;
96 control._options = props;
97
98 useIsomorphicLayoutEffect(() => {
99 const sub = control._subscribe({
100 formState: control._proxyFormState,
101 callback: () => updateFormState({ ...control._formState }),

Callers 15

form.test-d.tsFile · 0.90
watch.test-d.tsFile · 0.90
ComponentFunction · 0.90
AppFunction · 0.90
FieldArrayFunction · 0.90
FormFunction · 0.90
ComponentFunction · 0.90
ComponentFunction · 0.90
ComponentFunction · 0.90
AppFunction · 0.90
TestComponentFunction · 0.90

Calls 2

createFormControlFunction · 0.90
deepEqualFunction · 0.85

Tested by 15

ComponentFunction · 0.72
AppFunction · 0.72
FieldArrayFunction · 0.72
FormFunction · 0.72
ComponentFunction · 0.72
ComponentFunction · 0.72
ComponentFunction · 0.72
AppFunction · 0.72
TestComponentFunction · 0.72
ComponentFunction · 0.72
ComponentFunction · 0.72
ProviderFunction · 0.72