不要听,听他们回答。你只需要确保安装了
我只是想在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>
在这里你可以做一些类似的事情
<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')
};
npm i -D @types/react
问题是bcuzHTMLElement
没有onClick
事件处理程序
const anchorTag = (e:React.MouseEvent<HTMLAnchorElement>) =>{
e.preventDefault();
}
const buttonTag = (e:React.MouseEvent<HTMLButtonElement>) =>{
e.preventDefault();
}
您只需要确保使用具有HTMLAnchorElement、HTMLButtonElement的html标签