简体   繁体   English

onClick 无法分配给 TypeScript 反应中的类型“IntrinsicAttributes”错误

[英]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.

相关问题 Typescript 错误:类型'{ onClick: () =&gt; void; }' 不可分配给类型 'IntrinsicAttributes' - Typescript error: Type '{ onClick: () => void; }' is not assignable to type 'IntrinsicAttributes' 对于具有扩展的可区分联合的 React 组件,TypeScript 错误“不可分配给类型‘IntrinsicAttributes’” - TypeScript error "not assignable to type 'IntrinsicAttributes'" for React component with extended discriminated union 与 TypeScript 反应:类型不可分配给类型“IntrinsicAttributes” - React with TypeScript: Type is not assignable to type 'IntrinsicAttributes' TypeScript错误:类型“…”不能分配给类型“ IntrinsicAttributes&Number” - TypeScript error : Type '…' is not assignable to type 'IntrinsicAttributes & Number' React TypeScript 错误:TS2322 - 类型''不可分配给类型'IntrinsicAttributes &amp; String' - React TypeScript Error : TS2322 - Type '' is not assignable to type 'IntrinsicAttributes & String' 不可分配到类型“IntrinsicAttributes”(React.js 和 TypeScript.js) - is not assignable to type 'IntrinsicAttributes (React.js and TypeScript.js) 打字稿错误:类型&#39;{道具:IDrink; }&#39; 不可分配给类型 &#39;IntrinsicAttributes &amp; IDrink&#39; - Typescript Error: Type '{ props: IDrink; }' is not assignable to type 'IntrinsicAttributes & IDrink' 为什么它抱怨类型 {intrinsicattributes &amp;&amp; true} 或类型 {intrinsicattributes &amp;&amp; false} 不能使用 react 和 typescript 分配? - Why does it complain about type {intrinsicattributes && true} or type {intrinsicattributes && false} are not assignable using react and typescript? 样式组件 + typescript:“as”不可分配给类型 IntrinsicAttributes - Styled components + typescript: "as" is not assignable to type IntrinsicAttributes 不可分配到类型 IntrinsicAttributes 和 IntrinsicClassAttributes React.js - Not assignable to type IntrinsicAttributes & IntrinsicClassAttributes React.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM