简体   繁体   English

Typescript 错误:类型'{ onClick: () => void; }' 不可分配给类型 'IntrinsicAttributes'

[英]Typescript error: Type '{ onClick: () => void; }' is not assignable to type 'IntrinsicAttributes'

I'm new to Typescript and I'm having a hard time trying to understand why my code isn't working.我是 Typescript 的新手,我很难理解为什么我的代码不起作用。

I'm trying to build a carousel image gallery using react and typescript and i'm having problems when I try to use "onClick" on the left and right arrows for navigation.我正在尝试使用 react 和 typescript 构建轮播图像库,当我尝试在左右箭头上使用“onClick”进行导航时遇到问题。

I get the following errors:我收到以下错误:

Type '{ onClick: () => void; }'键入'{ onClick: () => void; }' '{ onClick: () => void; }' is not assignable to type 'IntrinsicAttributes' . '{ onClick: () => void; }'不可分配给类型'IntrinsicAttributes' Property 'onClick' does not exist on type 'IntrinsicAttributes' .类型'IntrinsicAttributes'上不存在属性'onClick' '。

Here's my code:这是我的代码:

const Carousel = ({ slides }) => {
   
    return (
        <section className="slider">

            <ArrowRight onClick = {nextSlide} /> //The error is here!
            <ArrowLeft onClick = { () => prevSlide() } /> //and here!

            {slides.map((slide, index) => {
                return (
                    <div className="img-slide">
                        <img src={slide.image} />
                        <div className="rolo-slide">
                            {/* <img src={slide.image} onClick={slides.setCurrent(index)} /> */}
                        </div>
                    </div>
                )
            })}
        </section>
    )
}
export default Carousel

Here's what ArrowLeft and Right are, they're just a span, I made the arrows using CSS.这是 ArrowLeft 和 Right 是什么,它们只是一个跨度,我使用 CSS 制作了箭头。

function ArrowLeft() {
    return (
        <span className="arrow left"></span>
    )
}

Your ArrowLeft doesn't accept any props at all, so TypeScript won't let you assign an onClick prop to it that it doesn't accept.您的ArrowLeft根本不接受任何道具,因此 TypeScript 不会让您为其分配不接受的onClick道具。

Update ArrowLeft so it accepts an onClick handler.更新ArrowLeft ,使其接受onClick处理程序。 For functional components I usually use function expressions rather than declarations:对于功能组件,我通常使用 function 表达式而不是声明:

const ArrowLeft: React.FC<{onClick?: React.MouseEventHandler<HTMLElement>}> = ({onClick}) => {
    return (
        <span className="arrow left" onClick={onClick}></span>
    );
};

Some notes on that:对此的一些说明:

  • React.FC is an alias for React.FunctionComponent . React.FCReact.FunctionComponent的别名。 It tells TypeScript that ArrowLeft is a function component.它告诉 TypeScript ArrowLeft是一个 function 组件。
  • React.FC has an optional type parameter to specify the type of the props that the functional component expects (they'll be combined with the usual props). React.FC有一个可选的类型参数来指定功能组件期望的道具的类型(它们将与通常的道具组合)。 The <> after React.FC provides a type argument describing the props object this functional component accepts. React.FC之后的<>提供了一个类型参数,用于描述此功能组件接受的道具 object。
  • onClick?: says the onClick prop is optional. onClick?:onClick道具是可选的。
  • React.MouseEventHandler<HTMLElement> says that the type of the prop (if it's given) is a mouse event handler for an HTML element. React.MouseEventHandler<HTMLElement>表示道具的类型(如果已给出)是 HTML 元素的鼠标事件处理程序。
  • Notice how I've forwarded the onClick handler on to the span so that it does get called when the span is clicked.请注意我如何将onClick处理程序转发到span上,以便在单击span时调用它。

I've used an arrow function because they're (slightly) lighter-weight than traditional functions, but you can use a traditional function if you prefer:我使用了箭头 function 因为它们比传统功能(稍微)更轻,但是如果您愿意,可以使用传统的 function:

const ArrowLeft: React.FC<{onClick?: React.MouseEventHandler<HTMLElement>}> = function({onClick}) {
    return (
        <span className="arrow left" onClick={onClick}></span>
    );
};

BTW: Don't worry about memorizing all of these types.顺便说一句:不要担心记住所有这些类型。 You'll want to know React.FC of course (or React.Component for class components), but for instance for the onClick above you don't have to magically know it.当然,您会想知道React.FC (或React.Component组件的class ),但例如对于上面的onClick ,您不必神奇地知道它。 In any decent IDE, you could start out with that not there and do this:在任何体面的 IDE 中,您可以从不存在的开始并执行以下操作:

// To start with
const ArrowLeft: React.FC = () => {
    return (
        <span className="arrow left" onClick={}></span>
    );
};

...then hover your mouse over the onClick part in the span . ...然后 hover 将鼠标悬停在span中的onClick部分上。 My IDE (VS Code) tells me that the type of onClick is React.MouseEventHandler<HTMLSpanElement> | undefined我的 IDE(VS 代码)告诉我 onClick 的类型是onClick React.MouseEventHandler<HTMLSpanElement> | undefined React.MouseEventHandler<HTMLSpanElement> | undefined : React.MouseEventHandler<HTMLSpanElement> | undefined

VS Code IDE 显示 span 的 onClick 的工具提示

The | undefined| undefined | undefined part I handled in the props by using ?我在道具中处理的| undefined部分使用? to make it optional, and I changed HTMLSpanElement to just the more general HTMLElement because there wasn't any need for the code using ArrowLeft to know that it uses a span , specifically, vs. a div or i or whatever.为了使其成为可选,我将HTMLSpanElement更改为更通用的HTMLElement ,因为使用ArrowLeft的代码不需要知道它使用span ,特别是与divi或其他任何东西相比。

暂无
暂无

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

相关问题 onClick 无法分配给 TypeScript 反应中的类型“IntrinsicAttributes”错误 - onClick not assignable to type 'IntrinsicAttributes' error in TypeScript React TypeScript错误:类型“…”不能分配给类型“ IntrinsicAttributes&Number” - TypeScript error : Type '…' is not assignable to type 'IntrinsicAttributes & Number' 输入&#39;{孩子:字符串; onClick: () =&gt; 无效; }&#39; 不可分配给类型 &#39;IntrinsicAttributes - Type '{ children: string; onClick: () => void; }' is not assignable to type 'IntrinsicAttributes 打字稿错误:类型&#39;{道具:IDrink; }&#39; 不可分配给类型 &#39;IntrinsicAttributes &amp; IDrink&#39; - Typescript Error: Type '{ props: IDrink; }' is not assignable to type 'IntrinsicAttributes & IDrink' 与 TypeScript 反应:类型不可分配给类型“IntrinsicAttributes” - React with TypeScript: Type is not assignable to type 'IntrinsicAttributes' 对于具有扩展的可区分联合的 React 组件,TypeScript 错误“不可分配给类型‘IntrinsicAttributes’” - TypeScript error "not assignable to type 'IntrinsicAttributes'" for React component with extended discriminated union 类型“{}”不可分配给类型“IntrinsicAttributes &amp; {}” - Type '{ }' is not assignable to type 'IntrinsicAttributes & {}' 样式组件 + typescript:“as”不可分配给类型 IntrinsicAttributes - Styled components + typescript: "as" is not assignable to type IntrinsicAttributes 类型“{}”不可分配给类型“IntrinsicAttributes” - Type '{}' is not assignable to type 'IntrinsicAttributes' TypeScript 错误:类型 &#39;type&#39; 不可分配给类型 &#39;IntrinsicAttributes &amp; &#39;type&#39; &amp; { children?: ReactNode; }&#39;。 如何解决? - TypeScript Error: Type 'type' is not assignable to type 'IntrinsicAttributes & 'type' & { children?: ReactNode; }'. How to fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM