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

Function NestedArray

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 8

useFieldArrayFunction · 0.85
appendFunction · 0.85
prependFunction · 0.85
updateFunction · 0.85
swapFunction · 0.85
moveFunction · 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…