Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Am I assigning a ref the right way? TS errors when using htmlAttributes={{ref: ...}} #916

Open
tremby opened this issue Aug 7, 2023 · 1 comment

Comments

@tremby
Copy link
Contributor

tremby commented Aug 7, 2023

In the docs it says I can attach a ref to the DOM element like this:

<Imgix htmlAttributes={{ ref: handleRef }}>

Here's what I'm doing, boiled down to the simplest case:

  const ref = useRef<HTMLImageElement>(null);
  return <Imgix htmlAttributes={{ref}} />;

TS complains:

No overload matches this call.
  Overload 1 of 2, '(props: SharedImgixAndSourceProps | Readonly<SharedImgixAndSourceProps>): Imgix', gave the following error.
    Type 'RefObject<HTMLImageElement>' is not assignable to type 'string'.
  Overload 2 of 2, '(props: SharedImgixAndSourceProps, context: any): Imgix', gave the following error.
    Type 'RefObject<HTMLImageElement>' is not assignable to type 'string'.

If I pass the ref I actually want to pass, which is a RefCallback, I get a different error:

  const { ref } = useResizeObserver<HTMLImageElement>();
  return <Imgix htmlAttributes={{ref}} />;
No overload matches this call.
  Overload 1 of 2, '(props: SharedImgixAndSourceProps | Readonly<SharedImgixAndSourceProps>): Imgix', gave the following error.
    Type 'RefCallback<HTMLImageElement>' is not assignable to type 'string'.
  Overload 2 of 2, '(props: SharedImgixAndSourceProps, context: any): Imgix', gave the following error.
    Type 'RefCallback<HTMLImageElement>' is not assignable to type 'string'.

I wonder if something is wrong with the types or implementation, or if I'm doing something wrong.

@tremby
Copy link
Contributor Author

tremby commented Aug 8, 2023

I should note that if I ignore the error, the functionality is working just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant