4
export const Test = (): JSX.Element => {
const wrapperRef = useRef(null);
useEffect(() => {
function handleClickOutside(event: MouseEvent) {
if (wrapperRef.current && !wrapperRef.current.contains(event.target)) {
console.log("Fora!");
} else {
console.log("Dentro!");
}
}
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, [wrapperRef]);
return (
<div ref={wrapperRef}>
)
};
Under "! wrapperRef.current." Typescript shows me this error:
(Property)
MutableRefObject<null>.current: null
"Object is possibly 'null'. ts(2531)
How do I fix this?