gpt4 book ai didi

reactjs - (事件 : MouseEvent) => void' 不可分配给类型 '() => Event'

转载 作者:搜寻专家 更新时间:2023-10-30 21:46:11 24 4
gpt4 key购买 nike

这看起来很简单,但我无法处理这个问题我总是在控制台中收到相同的消息:

Type error: Type '(event: MouseEvent) => void' is not assignable to type '() => Event'. TS2322

这是我的代码:

//in one class  
//some code
render()
return (
<div className={keyStyle} key={key} onClick={() => this.props.turnKey()}></div>
)

//in other classs
//some code
return (
<div className="container">
<Display />
<Keys turnKey={this.activateKey}/>
</div>
);
}

activateKey = (event : React.MouseEvent): void =>{
console.log(event.target);
}

我该如何解决这个错误?

最佳答案

您没有将 event(这是必要的)传递给您的 this.props.turnKey。我建议您按原样传递 this.props.turnKey:

<div className={keyStyle} key={key} onClick={this.props.turnKey}>

也许,它应该能解决您的问题。


如果不是,则 Prop this.props.turnKey 的声明存在一些问题。可能你是这样声明的

turnKey: () => Event

代替

turnKey: (event: React.MouseEvent) => void

关于reactjs - (事件 : MouseEvent<Element, MouseEvent>) => void' 不可分配给类型 '() => Event',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54537652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com