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

Function WatchUseFieldArray

app/src/watchUseFieldArray.tsx:11–114  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9};
10
11const WatchUseFieldArray: React.FC = () => {
12 const { mode } = useParams();
13 const { control, handleSubmit, reset, watch, register } = useForm<FormInputs>(
14 {
15 ...(mode === 'default'
16 ? {
17 defaultValues: {
18 data: [{ name: 'test' }, { name: 'test1' }, { name: 'test2' }],
19 },
20 }
21 : {}),
22 mode: mode === 'formState' ? 'onChange' : 'onSubmit',
23 },
24 );
25 const { fields, append, prepend, swap, move, insert, remove, update } =
26 useFieldArray({
27 control,
28 name: 'data',
29 });
30 const onSubmit = () => {};
31 const watchAll = watch('data') || [];
32
33 React.useEffect(() => {
34 setTimeout(() => {
35 if (mode === 'asyncReset') {
36 reset({
37 data: [{ name: 'test' }, { name: 'test1' }, { name: 'test2' }],
38 });
39 }
40 }, 10);
41 }, [reset, mode]);
42
43 renderCount++;
44
45 return (
46 <form onSubmit={handleSubmit(onSubmit)}>
47 <ul>
48 {fields.map((data, index) => (
49 <li key={data.id}>
50 <input
51 id={`field${index}`}
52 data-order={index}
53 {...register(`data.${index}.name` as const)}
54 />
55 <button id={`delete${index}`} onClick={() => remove(index)}>
56 Delete
57 </button>
58 </li>
59 ))}
60 </ul>
61
62 <button
63 id="append"
64 type="button"
65 onClick={() => append({ name: renderCount.toString() })}
66 >
67 append
68 </button>

Callers

nothing calls this directly

Calls 13

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

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…