[英]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.FC
是React.FunctionComponent
的别名。 It tells TypeScript that ArrowLeft
is a function component.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 元素的鼠标事件处理程序。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
:
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
,特别是与div
或i
或其他任何东西相比。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.