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

Function NestedArray

app/src/useWatchUseFieldArrayNested.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 value = useWatch({
31 name: 'test',
32 control,
33 });
34 const renderCountRef = React.useRef(0);
35 renderCountRef.current++;
36
37 return (
38 <div>
39 <div>
40 {fields.map((item, i) => (
41 <Controller
42 key={item.id}
43 render={({ field }) => <input {...field} aria-label={'name'} />}
44 name={`test.${index}.keyValue.${i}.name`}
45 control={control}
46 />
47 ))}
48 </div>
49
50 <button
51 id={`nest-append-${index}`}
52 type="button"
53 onClick={() => append({ name: 'append' })}
54 >
55 append
56 </button>
57
58 <button
59 id={`nest-prepend-${index}`}
60 type="button"
61 onClick={() => prepend({ name: 'prepend' })}
62 >
63 prepend
64 </button>
65
66 <button
67 id={`nest-swap-${index}`}
68 onClick={() => swap(1, 2)}
69 type="button"
70 >
71 swap
72 </button>
73
74 <button
75 id={`nest-move-${index}`}

Callers

nothing calls this directly

Calls 9

useFieldArrayFunction · 0.85
useWatchFunction · 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…