Skip to content

Commit 27a1313

Browse files
committed
Improved 72
1 parent 2ca5d6b commit 27a1313

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/08-advanced-patterns/72-as-prop-with-forward-ref.problem.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
ComponentPropsWithoutRef,
33
ElementType,
4+
ForwardedRef,
45
forwardRef,
56
useRef,
67
} from "react";
@@ -13,13 +14,14 @@ import { Equal, Expect } from "../helpers/type-utils";
1314
* So, don't feel bad if you don't find it at all.
1415
*/
1516

16-
export const UnwrappedLink = <T extends ElementType>(
17+
export const UnwrappedLink = <TAs extends ElementType>(
1718
props: {
18-
as?: T;
19-
} & ComponentPropsWithoutRef<ElementType extends T ? "a" : T>,
19+
as?: TAs;
20+
} & ComponentPropsWithoutRef<ElementType extends TAs ? "a" : TAs>,
21+
ref: ForwardedRef<any>,
2022
) => {
2123
const { as: Comp = "a", ...rest } = props;
22-
return <Comp {...rest}></Comp>;
24+
return <Comp {...rest} ref={ref}></Comp>;
2325
};
2426

2527
const Link = forwardRef(UnwrappedLink);

src/08-advanced-patterns/72-as-prop-with-forward-ref.solution.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { ComponentProps, ElementType, forwardRef, useRef } from "react";
1+
import {
2+
ComponentProps,
3+
ElementType,
4+
ForwardedRef,
5+
forwardRef,
6+
useRef,
7+
} from "react";
28
import { Equal, Expect } from "../helpers/type-utils";
39

410
// Added fixedForwardRef from a previous exercise
@@ -19,9 +25,10 @@ function UnwrappedLink<T extends ElementType>(
1925
props: {
2026
as?: T;
2127
} & DistributiveOmit<ComponentProps<ElementType extends T ? "a" : T>, "as">,
28+
ref: ForwardedRef<any>,
2229
) {
2330
const { as: Comp = "a", ...rest } = props;
24-
return <Comp {...rest}></Comp>;
31+
return <Comp {...rest} ref={ref}></Comp>;
2532
}
2633

2734
const Link = fixedForwardRef(UnwrappedLink);

0 commit comments

Comments
 (0)