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

Function useFieldArray

src/useFieldArray.ts:81–458  ·  view source on GitHub ↗
(
  props: UseFieldArrayProps<
    TFieldValues,
    TFieldArrayName,
    TKeyName,
    TTransformedValues
  >,
)

Source from the content-addressed store, hash-verified

79 * ```
80 */
81export function useFieldArray<
82 TFieldValues extends FieldValues = FieldValues,
83 TFieldArrayName extends FieldArrayPath<TFieldValues> =
84 FieldArrayPath<TFieldValues>,
85 TKeyName extends string = 'id',
86 TTransformedValues = TFieldValues,
87>(
88 props: UseFieldArrayProps<
89 TFieldValues,
90 TFieldArrayName,
91 TKeyName,
92 TTransformedValues
93 >,
94): UseFieldArrayReturn<TFieldValues, TFieldArrayName, TKeyName> {
95 const formControl = useFormControlContext<
96 TFieldValues,
97 any,
98 TTransformedValues
99 >();
100 const {
101 control = formControl,
102 name,
103 keyName = 'id',
104 shouldUnregister,
105 rules,
106 } = props;
107 const [fields, setFields] = React.useState(control._getFieldArray(name));
108 const ids = React.useRef<string[]>(
109 control._getFieldArray(name).map(generateId),
110 );
111
112 const _actioned = React.useRef(false);
113
114 control._names.array.add(name);
115
116 React.useMemo(
117 () =>
118 rules &&
119 fields.length >= 0 &&
120 (control as Control<TFieldValues, any, TTransformedValues>).register(
121 name as FieldPath<TFieldValues>,
122 rules as RegisterOptions<TFieldValues>,
123 ),
124 [control, name, fields.length, rules],
125 );
126
127 useIsomorphicLayoutEffect(
128 () =>
129 control._subjects.array.subscribe({
130 next: ({
131 values,
132 name: fieldArrayName,
133 }: {
134 values?: FieldValues;
135 name?: InternalFieldName;
136 }) => {
137 if (fieldArrayName === name || !fieldArrayName) {
138 const fieldValues = get(values, name);

Callers 15

FieldArrayFunction · 0.90
ComponentFunction · 0.90
FieldArrayFunction · 0.90
AppFunction · 0.90
AppFunction · 0.90
ComponentFunction · 0.90
AppFunction · 0.90
ChildFunction · 0.90
FormFunction · 0.90
ComponentFunction · 0.90
FieldArrayFunction · 0.90

Calls 5

useFormControlContextFunction · 0.90
unsetFunction · 0.85
cloneObjectFunction · 0.85
iterateFieldsByActionFunction · 0.85
updateMountedFunction · 0.70

Tested by 15

FieldArrayFunction · 0.72
ComponentFunction · 0.72
FieldArrayFunction · 0.72
AppFunction · 0.72
AppFunction · 0.72
ComponentFunction · 0.72
AppFunction · 0.72
ChildFunction · 0.72
FormFunction · 0.72
ComponentFunction · 0.72
FieldArrayFunction · 0.72
NestedFunction · 0.72