Closed rafgraph closed 3 years ago
Hello,
I’ve just checked out your reproduction code and noticed that you wanted to type localRef
as an HTMLElement
. Please type it as React.ElementRef<T>
instead.
Also, I recommend using react-merge-refs which offers a clean for this purpose:
import mergeRefs from "react-merge-refs";
import * as React from "react";
import type {
PolymorphicForwardRefExoticComponent,
PolymorphicPropsWithoutRef,
PolymorphicPropsWithRef,
} from "react-polymorphic-types";
import { HeadingDefaultElement, HeadingOwnProps } from "./Heading";
export type HeadingProps<
T extends React.ElementType = typeof HeadingDefaultElement
> = PolymorphicPropsWithRef<HeadingOwnProps, T>;
export const Heading: PolymorphicForwardRefExoticComponent<
HeadingOwnProps,
typeof HeadingDefaultElement
> = React.forwardRef(function Heading<
T extends React.ElementType = typeof HeadingDefaultElement
>(
{
as,
color,
style,
...restProps
}: PolymorphicPropsWithoutRef<HeadingOwnProps, T>,
ref: React.ForwardedRef<React.ElementRef<T>>
) {
const Element: React.ElementType = as || HeadingDefaultElement;
const localRef = React.useRef<React.ElementRef<T>>(null);
return (
<Element
ref={mergeRefs([localRef, ref])}
style={{ color, ...style }}
{...restProps}
/>
);
});
You may cast localRef as React.RefObject<Element>
or localRef as React.RefObject<HTMLElement>
when desired, but the latter isn’t guaranteed to work e.g. when using as="svg"
.
I’ve just simplified the docs about typing forwarded refs. You can avoid using unsafe as
casts by applying the change in https://github.com/kripod/react-polymorphic-types/commit/086bcc99751722d58be92241106b8f24a203c409.
Changing ref: React.ForwardedRef<React.ElementRef<T>>
to ref: React.ForwardedRef<Element>
as per https://github.com/kripod/react-polymorphic-types/commit/086bcc99751722d58be92241106b8f24a203c409 works great, thanks!
Also, good to know about Element
vs HTMLElement
when using with as="svg"
, thanks for the info.
Hi, great library, thanks for creating it! I'm using it in React Interactive
When reusing a forwarded
ref
I get this type error:As a workaround I replaced
ref: React.ForwardedRef<React.ElementRef<T>>
withref: React.ForwardedRef<HTMLElement>
.I could be typing
callbackRef
andlocalRef
wrong. I tried replacingHTMLElement
withElementRef<T>
, but that doesn't work with subtyping and leads to cascading errors wherever I uselocalRef
, for example when passinglocalRef.current
tosomeSubtypeFunction
I get errorType 'ElementRef<T>' is not assignable to type '{ tagName?: string | undefined; }'
.For a live reproduction see: https://codesandbox.io/s/polymorphic-reuse-ref-pjx00