ccl*_*lle 6 dom-events typescript reactjs typescript-typings
我有一个 keydown 处理函数,定义如下:
handleOnKeyDown = (e: React.KeyboardEvent<HTMLButtonElement>) => {
if (key.isEscape(e.key)) {
stopEvent(e);
this.props.handleClose(e);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在我的代码的另一部分中,将此函数传递给removeEventListenerand addEventListener:
componentDidMount () {
const { current: modal } = this.modalRef;
if (modal) {
modal.addEventListener('keydown', this.handleOnKeyDown);
}
}
componentWillUnmount () {
const { current: modal } = this.modalRef;
if (modal) {
modal.removeEventListener('keydown', this.handleOnKeyDown);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下打字稿错误:
handleOnKeyDown = (e: React.KeyboardEvent<HTMLButtonElement>) => {
if (key.isEscape(e.key)) {
stopEvent(e);
this.props.handleClose(e);
}
}
Run Code Online (Sandbox Code Playgroud)
什么会解决这个问题?
小智 2
这个问题似乎已经回答了这个问题: react typescript issues on addEvenListener
addEventListenerand的处理函数removeEventListener不接受 aReact.KeyboardEvent作为参数。它需要一个 DOM 事件。
| 归档时间: |
|
| 查看次数: |
1612 次 |
| 最近记录: |