如何使用 mouseEvent 和 event.target 键入事件(使用打字稿反应项目)?
How can I type an event with mouseEvent and event.target (react project with typescript)?
我的项目需要你的帮助。
我想在单击外部时关闭菜单。
我写了下面的代码,但是打字稿有问题。
当我使用 any 键入“ev”参数时,代码有效,但我知道不推荐这样做。
这是我的代码:
useEffect(() => {
const checkIfClickedOutside = (ev: any) => {
if (isMenuOpen && menuRef.current && !menuRef.current?.contains(ev.target)) {
setIsMenuOpen(false);
}
};
document.addEventListener('mousedown', checkIfClickedOutside);
return () => {
document.removeEventListener('mousedown', checkIfClickedOutside);
};
}, [isMenuOpen, menuRef]);
我尝试使用 MouseEvent,但出现此错误:
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.
还有这个:
const checkIfClickedOutside: (ev: MouseEvent) => void
No overload matches this call.
Overload 1 of 2, '(type: "mousedown", listener: (this: Document, ev: MouseEvent) => any, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
Argument of type '(ev: MouseEvent) => void' is not assignable to parameter of type '(this: Document, ev: MouseEvent) => any'.
Types of parameters 'ev' and 'ev' are incompatible.
Type 'MouseEvent' is missing the following properties from type 'MouseEvent<Element, MouseEvent>': nativeEvent, isDefaultPrevented, isPropagationStopped, persist
Overload 2 of 2, '(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
Argument of type '(ev: MouseEvent) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
Type '(ev: MouseEvent) => void' is not assignable to type 'EventListener'.
Types of parameters 'ev' and 'evt' are incompatible.
Type 'Event' is missing the following properties from type 'MouseEvent<Element, MouseEvent>': altKey, button, buttons, clientX, and 18 more
有人可以帮助我吗?非常感谢!
当您将 addEventListener
与 mousedown
参数一起使用时,Official DOM typings point to a MouseEvent 界面。
我的项目需要你的帮助。
我想在单击外部时关闭菜单。
我写了下面的代码,但是打字稿有问题。
当我使用 any 键入“ev”参数时,代码有效,但我知道不推荐这样做。
这是我的代码:
useEffect(() => {
const checkIfClickedOutside = (ev: any) => {
if (isMenuOpen && menuRef.current && !menuRef.current?.contains(ev.target)) {
setIsMenuOpen(false);
}
};
document.addEventListener('mousedown', checkIfClickedOutside);
return () => {
document.removeEventListener('mousedown', checkIfClickedOutside);
};
}, [isMenuOpen, menuRef]);
我尝试使用 MouseEvent,但出现此错误:
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.
还有这个:
const checkIfClickedOutside: (ev: MouseEvent) => void
No overload matches this call.
Overload 1 of 2, '(type: "mousedown", listener: (this: Document, ev: MouseEvent) => any, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
Argument of type '(ev: MouseEvent) => void' is not assignable to parameter of type '(this: Document, ev: MouseEvent) => any'.
Types of parameters 'ev' and 'ev' are incompatible.
Type 'MouseEvent' is missing the following properties from type 'MouseEvent<Element, MouseEvent>': nativeEvent, isDefaultPrevented, isPropagationStopped, persist
Overload 2 of 2, '(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
Argument of type '(ev: MouseEvent) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
Type '(ev: MouseEvent) => void' is not assignable to type 'EventListener'.
Types of parameters 'ev' and 'evt' are incompatible.
Type 'Event' is missing the following properties from type 'MouseEvent<Element, MouseEvent>': altKey, button, buttons, clientX, and 18 more
有人可以帮助我吗?非常感谢!
addEventListener
与 mousedown
参数一起使用时,Official DOM typings point to a MouseEvent 界面。