Skip to content

Commit 716502d

Browse files
committed
Improved explainer
1 parent 616b92d commit 716502d

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

src/09-external-libraries/73-react-hook-form.explainer.tsx

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,27 @@ import { Equal, Expect } from "../helpers/type-utils";
77
*
88
* Investigate why this is, and what TFieldValues is being used for.
99
*/
10-
const formWithValues = useForm({
11-
defaultValues: {
12-
firstName: "",
13-
lastName: "",
14-
},
15-
});
10+
const Example1 = () => {
11+
const form = useForm({
12+
defaultValues: {
13+
firstName: "",
14+
lastName: "",
15+
},
16+
});
1617

17-
const values = formWithValues.getValues();
18-
19-
type test = Expect<
20-
Equal<typeof values, { firstName: string; lastName: string }>
21-
>;
18+
return (
19+
<form
20+
onSubmit={form.handleSubmit((values) => {
21+
type test = Expect<
22+
Equal<typeof values, { firstName: string; lastName: string }>
23+
>;
24+
})}
25+
>
26+
<input {...form.register("firstName")} />
27+
<input {...form.register("lastName")} />
28+
</form>
29+
);
30+
};
2231

2332
/**
2433
* 2. When you don't pass a default value, the return type of getValues is
@@ -27,8 +36,17 @@ type test = Expect<
2736
* Investigate why this is, and what type FieldValues is.
2837
*/
2938

30-
const formWithoutValues = useForm();
31-
32-
const values2 = formWithoutValues.getValues();
39+
const Example2 = () => {
40+
const form = useForm({});
3341

34-
type test2 = Expect<Equal<typeof values2, FieldValues>>;
42+
return (
43+
<form
44+
onSubmit={form.handleSubmit((values) => {
45+
type test = Expect<Equal<typeof values, FieldValues>>;
46+
})}
47+
>
48+
<input {...form.register("firstName")} />
49+
<input {...form.register("lastName")} />
50+
</form>
51+
);
52+
};

0 commit comments

Comments
 (0)