Skip to content

Commit aa556e7

Browse files
committed
Added more tests to forwardRef explainer
1 parent 59cbbfd commit aa556e7

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/08-advanced-patterns/65-forward-ref-with-generics.explainer.1.tsx

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ForwardedRef, forwardRef } from "react";
1+
import { ForwardedRef, forwardRef, useRef } from "react";
22
import { Equal, Expect } from "../helpers/type-utils";
33

44
type Props<T> = {
@@ -31,20 +31,36 @@ type Props<T> = {
3131
* By doing it this way, we preserve the generic context of the function
3232
* being passed in.
3333
*/
34-
export const Table = <T,>(props: Props<T>, ref: ForwardedRef<any>) => {
35-
return null;
34+
export const Table = <T,>(
35+
props: Props<T>,
36+
ref: ForwardedRef<HTMLTableElement>,
37+
) => {
38+
return <table ref={ref} />;
3639
};
3740

3841
const ForwardReffedTable = forwardRef(Table);
3942

4043
const Parent = () => {
44+
const tableRef = useRef<HTMLTableElement>(null);
45+
const wrongRef = useRef<HTMLDivElement>(null);
4146
return (
42-
<ForwardReffedTable
43-
data={["123"]}
44-
renderRow={(row) => {
45-
type test = Expect<Equal<typeof row, string>>;
46-
return <div>123</div>;
47-
}}
48-
></ForwardReffedTable>
47+
<>
48+
<ForwardReffedTable
49+
ref={tableRef}
50+
data={["123"]}
51+
renderRow={(row) => {
52+
type test = Expect<Equal<typeof row, string>>;
53+
return <div>123</div>;
54+
}}
55+
/>
56+
<ForwardReffedTable
57+
// @ts-expect-error
58+
ref={wrongRef}
59+
data={["123"]}
60+
renderRow={(row) => {
61+
return <div>123</div>;
62+
}}
63+
/>
64+
</>
4965
);
5066
};

0 commit comments

Comments
 (0)