[英]onClick not assignable to type 'IntrinsicAttributes' error in TypeScript React
I have this code for a component:我有一个组件的代码:
Feedback.tsx反馈.tsx
import React, { useState } from 'react';
import './Feedback.css';
import FeedbackButton from './FeedbackButton';
function Feedback() {
const [isOpen, setIsOpen] = useState<boolean>(false)
return (
<div className="Feedback">
<FeedbackButton onClick={() => setIsOpen(!isOpen)} />
</div>
);
}
export default Feedback;
The FeedbackButton
component looks like this: FeedbackButton
组件如下所示:
FeedbackButton.tsx反馈按钮.tsx
import React from 'react';
import feedbackicon from './feedback.svg';
import './Feedback.css';
function FeedbackButton() {
return (
<button className="Feedback-button" type="button">
<img src={feedbackicon} className="Feedback-button-icon" alt="feedback" />
</button>
);
}
export default FeedbackButton;
On the line <FeedbackButton onClick={() => setIsOpen(!isOpen)} />
I get the error Type '{ onClick: () => void; }' is not assignable to type 'IntrinsicAttributes'. Property 'onClick' does not exist on type 'IntrinsicAttributes'.
在<FeedbackButton onClick={() => setIsOpen(!isOpen)} />
行我收到错误Type '{ onClick: () => void; }' is not assignable to type 'IntrinsicAttributes'. Property 'onClick' does not exist on type 'IntrinsicAttributes'.
Type '{ onClick: () => void; }' is not assignable to type 'IntrinsicAttributes'. Property 'onClick' does not exist on type 'IntrinsicAttributes'.
I searched it up and tried to fix it using FunctionalComponent, but I got the same error.我搜索了它并尝试使用 FunctionalComponent 修复它,但我得到了同样的错误。 How can I make this onClick with useState work?如何使这个 onClick 与 useState 一起工作?
function FeedbackButton(props:{onClick:()=>void}) {
return (
<button className="Feedback-button" type="button" onClick={props.onClick}>
<img src={feedbackicon} className="Feedback-button-icon" alt="feedback" />
</button>
);
}
something like this should work for you even better you can copy the type from button onclick to your props and have better typing if needed像这样的东西应该更适合你你可以将类型从按钮 onclick 复制到你的道具,如果需要的话可以更好地打字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.