I have a working code which handles an outside click in my custom dropdown element.
I just can't make the TypeScript stop complaining.
It looks like this:
const nodeRef = useRef<HTMLDivElement>(null);
const handleClick = (e: MouseEvent) => {
if (nodeRef.current !== null && nodeRef.current.contains(e.target)) {
return;
}
setShowContent(false);
};
useLayoutEffect(() => {
document.addEventListener("mousedown", handleClick);
return () => {
document.removeEventListener("mousedown", handleClick);
};
}, []);
My TS Error looks like this:
Error:(58, 62) TS2345: Argument of type 'EventTarget | null' is not assignable to parameter of type 'Node | null'.
Type 'EventTarget' is missing the following properties from type 'Node': baseURI, childNodes, firstChild, isConnected, and 44 more.