Muh*_*ric 6 typescript reactjs
看起来很简单,但我无法处理这个问题我总是在控制台中收到相同的消息:
类型错误:类型 '(event: MouseEvent) => void' 不可分配给类型 '() => 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);
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个错误?
您没有将event(这是必要的)传递给您的this.props.turnKey. 我建议您按this.props.turnKey原样通过:
<div className={keyStyle} key={key} onClick={this.props.turnKey}>
Run Code Online (Sandbox Code Playgroud)
也许,它应该可以解决您的问题。
如果没有,那么 prop 的声明存在一些问题this.props.turnKey。可能,你是这样声明的
turnKey: () => Event
Run Code Online (Sandbox Code Playgroud)
代替
turnKey: (event: React.MouseEvent) => void
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11867 次 |
| 最近记录: |