I have a click handler that I want to listen to any clicks on the window outside of my component:
useEffect(() => {
const handleOutSideClick = (event) => {
if (!ref.current?.contains(event.target)) {
setNavActive(false)
}
};
window.addEventListener('keydown', handleOutSideClick, false);
return () => {
window.removeEventListener('keydown', handleOutSideClick, false);
};
}, [ref]);
However, I am getting the error:
Parameter 'event' implicitly has an 'any' type.
(line 2)
and
Property 'contains' does not exist on type 'never'.
(line 3)
How do I specify type in order to get this click handler working?
New contributor
user22070661 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1