MCPcopy Index your code
hub / github.com/react-hook-form/react-hook-form / NestedArray

Function NestedArray

app/src/useFieldArrayNested.tsx:18–119  ·  view source on GitHub ↗
({
  control,
  index,
}: {
  control: Control<FormValues>;
  index: number;
})

Source from the content-addressed store, hash-verified

16};
17
18function NestedArray({
19 control,
20 index,
21}: {
22 control: Control<FormValues>;
23 index: number;
24}) {
25 const { fields, append, prepend, swap, move, remove, insert, update } =
26 useFieldArray<FormValues, 'test.0.keyValue'>({
27 name: `test.${index}.keyValue` as 'test.0.keyValue',
28 control,
29 });
30 const { touchedFields, dirtyFields } = useFormState({
31 control,
32 });
33 const renderCountRef = React.useRef(0);
34 renderCountRef.current++;
35
36 return (
37 <div>
38 <ul>
39 {fields.map((item, i) => (
40 <Controller
41 key={item.id}
42 render={({ field }) => <input {...field} aria-label={'name'} />}
43 name={`test.${index}.keyValue.${i}.name`}
44 control={control}
45 />
46 ))}
47 </ul>
48
49 <div id={`dirty-nested-${index}`}>{JSON.stringify(dirtyFields)}</div>
50 <div id={`touched-nested-${index}`}>{JSON.stringify(touchedFields)}</div>
51
52 <button
53 id={`nest-append-${index}`}
54 type="button"
55 onClick={() => append({ name: 'append' })}
56 >
57 append
58 </button>
59
60 <button
61 id={`nest-prepend-${index}`}
62 type="button"
63 onClick={() => prepend({ name: 'prepend' })}
64 >
65 prepend
66 </button>
67
68 <button
69 id={`nest-swap-${index}`}
70 onClick={() => swap(1, 2)}
71 type="button"
72 >
73 swap
74 </button>
75

Callers

nothing calls this directly

Calls 9

useFieldArrayFunction · 0.85
useFormStateFunction · 0.85
appendFunction · 0.85
prependFunction · 0.85
swapFunction · 0.85
moveFunction · 0.85
updateFunction · 0.85
removeFunction · 0.85
insertFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…