【发布时间】:2022-12-02 22:34:06
【问题描述】:
我从反应导入MouseEvent
import { MouseEvent } from 'react';
在下面使用MouseEvent
const closeSelectBox = (e: MouseEvent): void => {
if (!searchOptionWrapRef.current?.contains(e.target)) {
setOpenSelectBox(false)
}
};
我听我的closeSelectBox
useEffect(() => {
document.addEventListener("click", closeSelectBox);
return () => {
document.removeEventListener("click", closeSelectBox);
};
}, [])
searchOptionWrapRef 是一个div
const searchOptionWrapRef = useRef<HTMLDivElement>(null);
<div ref={searchOptionWrapRef}/>
但我收到以下错误
Argument of type 'EventTarget' is not assignable to parameter of type 'Node'.
Type 'EventTarget' is missing the following properties from type 'Node': baseURI, childNodes, firstChild, isConnected, and 43 more.
如何在不使用 any 代替 MouseEvent 的情况下解决此类型错误?
【问题讨论】:
标签: reactjs typescript