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

Function useController

src/useController.ts:50–284  ·  view source on GitHub ↗
(
  props: UseControllerProps<TFieldValues, TName, TTransformedValues>,
)

Source from the content-addressed store, hash-verified

48 * ```
49 */
50export function useController<
51 TFieldValues extends FieldValues = FieldValues,
52 TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
53 TTransformedValues = TFieldValues,
54>(
55 props: UseControllerProps<TFieldValues, TName, TTransformedValues>,
56): UseControllerReturn<TFieldValues, TName> {
57 const formControl = useFormControlContext<
58 TFieldValues,
59 any,
60 TTransformedValues
61 >();
62 const {
63 name,
64 disabled,
65 control = formControl,
66 shouldUnregister,
67 defaultValue,
68 exact = true,
69 } = props;
70 const isArrayField = isNameInFieldArray(control._names.array, name);
71
72 const defaultValueMemo = React.useMemo(
73 () =>
74 get(
75 control._formValues,
76 name,
77 get(control._defaultValues, name, defaultValue),
78 ),
79 [control, name, defaultValue],
80 );
81
82 const value = useWatch({
83 control,
84 name,
85 defaultValue: defaultValueMemo,
86 exact,
87 }) as FieldPathValue<TFieldValues, TName>;
88
89 const formState = useFormState({
90 control,
91 name,
92 exact,
93 });
94
95 const _props = React.useRef(props);
96 const _proxyRef = React.useRef<any>(null);
97
98 const _registerProps = React.useRef(
99 control.register(name, {
100 ...props.rules,
101 value,
102 ...(isBoolean(props.disabled) ? { disabled: props.disabled } : {}),
103 }),
104 );
105
106 _props.current = props;
107

Callers 15

ControllerFunction · 0.90
ChildFunction · 0.90
FormFunction · 0.90
AppFunction · 0.90
TestComponentFunction · 0.90
ChildFunction · 0.90
AppFunction · 0.90
ComponentFunction · 0.90
LoginFieldFunction · 0.90
TestFunction · 0.90

Calls 5

useFormControlContextFunction · 0.90
useWatchFunction · 0.90
useFormStateFunction · 0.90
cloneObjectFunction · 0.85
updateMountedFunction · 0.70

Tested by 15

ChildFunction · 0.72
FormFunction · 0.72
AppFunction · 0.72
TestComponentFunction · 0.72
ChildFunction · 0.72
AppFunction · 0.72
ComponentFunction · 0.72
LoginFieldFunction · 0.72
TestFunction · 0.72
Test1Function · 0.72
AppFunction · 0.72
InputFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…