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

Function UseFieldArray

app/src/useFieldArray.tsx:9–339  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

7type FormValues = { data: { name: string }[] };
8
9const UseFieldArray: React.FC = () => {
10 const { mode } = useParams();
11 const withoutFocus: boolean = mode === 'defaultAndWithoutFocus';
12 const {
13 control,
14 handleSubmit,
15 register,
16 formState: { isDirty, touchedFields, isValid, dirtyFields, errors },
17 reset,
18 } = useForm<FormValues>({
19 ...(mode === 'default' || withoutFocus
20 ? {
21 defaultValues: {
22 data: [{ name: 'test' }, { name: 'test1' }, { name: 'test2' }],
23 },
24 }
25 : {}),
26 mode: mode === 'formState' ? 'onChange' : 'onSubmit',
27 });
28 const {
29 fields,
30 append,
31 prepend,
32 swap,
33 move,
34 insert,
35 remove,
36 update,
37 replace,
38 } = useFieldArray({
39 control,
40 name: 'data',
41 });
42 const [data, setData] = React.useState<FormValues>();
43 const onSubmit = (data: FormValues) => {
44 setData(data);
45 };
46
47 React.useEffect(() => {
48 setTimeout(() => {
49 if (mode === 'asyncReset') {
50 reset({
51 data: [{ name: 'test' }, { name: 'test1' }, { name: 'test2' }],
52 });
53 }
54 }, 10);
55 }, [reset, mode]);
56
57 renderCount++;
58
59 return (
60 <form onSubmit={handleSubmit(onSubmit)}>
61 <ul>
62 {fields.map((data, index) => (
63 <li key={data.id}>
64 {index % 2 ? (
65 <input
66 id={`field${index}`}

Callers

nothing calls this directly

Calls 13

useFormFunction · 0.85
useFieldArrayFunction · 0.85
resetFunction · 0.85
handleSubmitFunction · 0.85
registerFunction · 0.85
removeFunction · 0.85
appendFunction · 0.85
prependFunction · 0.85
swapFunction · 0.85
moveFunction · 0.85
updateFunction · 0.85
replaceFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…