首页 > 解决方案 > React Link / href 的 Typescript onclick 事件类型 - 类型“IntrinsicAttributes 和 LinkProps”上不存在属性“onClick”

问题描述

我只是想在 TS 中添加一个简单的点击事件。

功能

  const displayNewClaim = (event: React.MouseEvent<HTMLElement>) => {
    event.preventDefault();
  };

TS错误

Type '{ children: string; to: string; styles: "button"; onClick: (event: MouseEvent<HTMLElement, MouseEvent>) => void; }' is not assignable to type 'IntrinsicAttributes & LinkProps & { children?: ReactNode; }'.
  Property 'onClick' does not exist on type 'IntrinsicAttributes & LinkProps & { children?: ReactNode; }'.  TS2322

    81 |           <input type="file" name="fileupload" /> <button>Upload CSV</button>
    82 |         </form>
  > 83 |         <Link to="/new-claim" styles="button" onClick={displayNewClaim}>
       |                                               ^
    84 |           New claim
    85 |         </Link>
    86 |       </Header>

标签: javascriptreactjstypescript

解决方案


在这里你可以做类似的事情

 <Link styles="button" onClick={(e) => displayNewClaim(e)}>New claim</Link>

功能

进口

import { navigate } from "gatsby"

功能

const displayNewClaim = (event: React.MouseEvent<HTMLElement>) => {
    event.preventDefault();

  navigate('/new-claim')
  };

推荐阅读