简体   繁体   English

在 NextJS Image 组件上设置 ref 会产生 typescript 错误

[英]Setting a ref on the NextJS Image component creates a typescript error

I'm trying to refactor one of my components to use typescript. As you see below, I'm using a reference on the NextJS Image component and trying to set the type我正在尝试重构我的一个组件以使用 typescript。如下所示,我正在使用 NextJS Image 组件上的引用并尝试设置类型

import { useRef } from 'react'
import Image, { ImageProps } from 'next/image'


type BannerBlockProps = {
  image?: ImageProps[]
}

const BannerBlock = (props: BannerBlockProps) => {
  const imageRef = useRef<HTMLImageElement >(null)

  return (
  
        <div className='BannerBlock-imageWrapper' >
          <Image className="BannerBlock-image" src={image[0].src} alt={image[0].alt} width={image[0].width} height={image[0].height} ref={imageRef}/>
        </div>
  )
}

export default BannerBlock

I then get the following typescript error on the Image ref property: :然后我在 Image ref 属性上收到以下typescript 错误::

(property) ref: RefObject Type '{ className: string; (属性) ref: RefObject Type '{ className: string; src: string |来源:字符串 | StaticImport;静态导入; alt: string |替代:字符串 | undefined;不明确的; width: string |宽度:字符串 | number |编号 | undefined;不明确的; height: string |高度:字符串 | number |编号 | undefined;不明确的; ref: RefObject;参考:参考对象; }' is not assignable to type 'IntrinsicAttributes & Omit<DetailedHTMLProps<ImgHTMLAttributes, HTMLImageElement>, "src" |... 4 more... | }' 不可分配给类型 'IntrinsicAttributes & Omit<DetailedHTMLProps<ImgHTMLAttributes, HTMLImageElement>, "src" |... 4 更多... | "loading"> & {...; “加载中”>&{...; }'. }'。 Property 'ref' does not exist on type 'IntrinsicAttributes & Omit<DetailedHTMLProps<ImgHTMLAttributes, HTMLImageElement>, "src" |... 4 more... |类型“IntrinsicAttributes & Omit<DetailedHTMLProps<ImgHTMLAttributes, HTMLImageElement> 上不存在属性“ref”,“src” |... 4 更多... | "loading"> & { “加载”>&{

It happens because in next/dist/client/image.d.ts the ref type is omitted on the ImageProps:发生这种情况是因为在 next/dist/client/image.d.ts 中 ImageProps 上省略了 ref 类型:

export declare type ImageProps = Omit<JSX.IntrinsicElements['img'], 'src' | 'srcSet' | 'ref' | 'width' | 'height' | 'loading'> & {
    src: string | StaticImport;
    width?: number | string;
    height?: number | string;
    layout?: LayoutValue;
    loader?: ImageLoader;
    quality?: number | string;
    priority?: boolean;
    loading?: LoadingValue;
    lazyRoot?: React.RefObject<HTMLElement> | null;
    lazyBoundary?: string;
    placeholder?: PlaceholderValue;
    blurDataURL?: string;
    unoptimized?: boolean;
    objectFit?: ImgElementStyle['objectFit'];
    objectPosition?: ImgElementStyle['objectPosition'];
    onLoadingComplete?: OnLoadingComplete;
};

The error disappears when I remove | 'ref'当我删除| 'ref'时错误消失了| 'ref' from the list of omitted properties.省略属性列表中的| 'ref' Why is this property omitted?为什么省略此属性? Is it bad practice to use the ref property on an Image component?在 Image 组件上使用ref属性是不好的做法吗? It's easily fixed putting a wrapper div on the image component and add the reference there, but I am curious to why this is not possible and if I'm doing this right.在图像组件上放置一个包装 div 并在那里添加引用很容易解决,但我很好奇为什么这是不可能的,以及我是否做对了。

Passing ref to next/image seems to be added in next 13.0.6 .将 ref 传递给 next/image 似乎是在 next 13.0.6中添加的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM