I have a Backdrop_DIV
which is rendered based on a open
for a Dropdown component.
{open &&
<LS.Backdrop_DIV
onClick={handleBackdropClick}
ref={backdrop_ref}
>
Backdrop
</LS.Backdrop_DIV>
}
I want the Backdrop_DIV
to go away if the user scrolls (touchmove
).
Obs: This is a mobile view.
const handleTouchMove = useCallback(()=>{
setOpen(false);
},[]);
useEffect(() => {
if (open) {
// ATTACHING THE EVENT LISTENR
backdrop_ref.current.addEventListener('touchmove', handleTouchMove );
}
// ATTEMPT TO REMOVE THE EVENT LISTENER
return () =>
backdrop_ref.current.removeEventListener('touchmove', handleTouchMove);
},[open,handleScroll]);
It works, but if fails when I'm trying to clear the event listener in the return of my useEffect
. Is there a way to do this?
Error:
react-dom.development.js:20313 Uncaught TypeError: Cannot read property 'removeEventListener' of null
This error is pretty obvious, because the Backdrop_DIV
is no longer mounted when it runs.
QUESTION
Do I need to bother removing the event listener in this case? What can I do?