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

Function SetValueWithSchema

app/src/setValueWithSchema.tsx:17–103  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

15 .required();
16
17const SetValueWithSchema: React.FC = () => {
18 const {
19 register,
20 setValue,
21 handleSubmit,
22 formState: { errors },
23 } = useForm<{
24 firstName: string;
25 lastName: string;
26 age: string;
27 checkbox: boolean;
28 radio: string;
29 select: string;
30 multiple: string[];
31 requiredField: string;
32 }>({
33 resolver: yupResolver(validationSchema),
34 });
35
36 renderCounter++;
37
38 useEffect(() => {
39 register('firstName', { required: true });
40 register('lastName', { required: true });
41 }, [register]);
42
43 return (
44 <form
45 onSubmit={handleSubmit((d) => {
46 console.log(d);
47 })}
48 >
49 <input
50 name="firstName"
51 placeholder="firstName"
52 onChange={(e) => {
53 setValue('firstName', e.target.value, {
54 shouldValidate: true,
55 shouldDirty: true,
56 });
57 }}
58 />
59 {errors.firstName && <p>firstName error</p>}
60
61 <input
62 name="lastName"
63 placeholder="lastName"
64 onChange={(e) => {
65 setValue('lastName', e.target.value, {
66 shouldValidate: true,
67 shouldDirty: true,
68 });
69 }}
70 />
71 {errors.lastName && <p>lastName error</p>}
72
73 <input
74 {...register('age')}

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