MCPcopy
hub / github.com/react-hook-form/react-hook-form / useForm

Function useForm

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

Source from the content-addressed store, hash-verified

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

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 3

createFormControlFunction · 0.90
cloneObjectFunction · 0.85
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

Used in the wild real call sites across dependent graphs

searching dependent graphs…