MCPcopy Create free account
hub / github.com/react-hook-form/react-hook-form / SetValue

Function SetValue

app/src/setValue.tsx:6–142  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4let renderCounter = 0;
5
6const SetValue: React.FC = () => {
7 const {
8 register,
9 setValue,
10 handleSubmit,
11 formState: { errors },
12 } = useForm<{
13 firstName: string;
14 lastName: string;
15 age: string;
16 trigger: string;
17 checkbox: boolean;
18 checkboxArray: string[];
19 radio: string;
20 select: string;
21 multiple: string[];
22 array: string[];
23 object: {
24 firstName: string;
25 lastName: string;
26 middleName: string;
27 };
28 nestedValue: string[];
29 }>();
30
31 useEffect(() => {
32 register('lastName', { required: true });
33 setValue('firstName', 'wrong', { shouldDirty: true });
34 setValue('age', '2', { shouldDirty: true });
35 setValue('trigger', '', { shouldDirty: true, shouldValidate: true });
36 setValue('checkbox', true, { shouldDirty: true });
37 setValue('checkboxArray', ['2', '3'], {
38 shouldDirty: true,
39 shouldValidate: true,
40 });
41 setValue('radio', 'radio', { shouldDirty: true });
42 setValue('select', 'a', { shouldDirty: true });
43 setValue('multiple', ['a', 'b'], { shouldDirty: true });
44 setValue('array', ['array.0', 'array.1', 'array.2'], {
45 shouldDirty: true,
46 });
47 setValue(
48 'object',
49 {
50 firstName: 'firstName',
51 lastName: 'lastName',
52 middleName: 'middleName',
53 },
54 { shouldDirty: true },
55 );
56 setValue('nestedValue', [], {
57 shouldValidate: true,
58 shouldDirty: true,
59 });
60 }, [register, setValue]);
61
62 renderCounter++;
63

Callers

nothing calls this directly

Calls 4

useFormFunction · 0.85
registerFunction · 0.85
setValueFunction · 0.85
handleSubmitFunction · 0.85

Tested by

no test coverage detected