我应该如何为mousedown
和touchstart
事件指定TypeScript类型?
function useOnClickOutside(ref: React.RefObject<any>, handler: () => void) {
React.useEffect(() => {
const listener = (event: React.TouchEvent | React.MouseEvent) => {
if (!ref || !ref.current) {
return;
}
const elem = document.getElementsByClassName(uniqueClassName)[0];
if (elem.contains(event.target)) {
return;
}
handler();
};
document.addEventListener("mousedown", listener);
document.addEventListener("touchstart", listener);
return () => {
document.removeEventListener("mousedown", listener);
document.removeEventListener("touchstart", listener);
};
}, [ref, handler]);
}
(event: MouseEvent | TouchEvent) =>
,因为你没有使用React来添加事件监听器。 - HMR