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

Function SetValueWithTrigger

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

Source from the content-addressed store, hash-verified

4let renderCounter = 0;
5
6const SetValueWithTrigger: React.FC = () => {
7 const {
8 register,
9 setValue,
10 handleSubmit,
11 formState: { errors },
12 } = useForm<{
13 firstName: string;
14 lastName: string;
15 }>();
16
17 useEffect(() => {
18 register('firstName', {
19 required: 'required',
20 minLength: {
21 value: 10,
22 message: 'minLength 10',
23 },
24 });
25 register('lastName', {
26 validate: (data) => {
27 if (data === 'bill') {
28 return true;
29 }
30
31 if (data && data.length < 10) {
32 return 'too short';
33 }
34
35 return 'error message';
36 },
37 });
38 }, [register]);
39
40 renderCounter++;
41
42 return (
43 <form onSubmit={handleSubmit(() => {})}>
44 <input
45 name="firstName"
46 placeholder="firstName"
47 onChange={(e) =>
48 setValue('firstName', e.target.value, {
49 shouldValidate: true,
50 shouldDirty: true,
51 })
52 }
53 />
54 {errors.firstName && <p>{errors.firstName.message}</p>}
55
56 <input
57 name="lastName"
58 placeholder="lastName"
59 onChange={(e) =>
60 setValue('lastName', e.target.value, {
61 shouldValidate: true,
62 shouldDirty: true,
63 })

Callers

nothing calls this directly

Calls 4

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

Tested by

no test coverage detected