I wanted to make the modal popup if someone leaves the window.
I tried the following code:
React.useEffect(() => {
const popupWhenLeave = (event) => {
if (
event.clientY <= 0 ||
event.clientX <= 0 ||
event.clientX >= window.innerWidth ||
event.clientY >= window.innerHeight
) {
handleClickOpen();
}
};
document.addEventListener("mouseleave", popupWhenLeave);
return () => {
document.removeEventListener("mouseleave", popupWhenLeave);
};
}, []);
The above code helped me but became annoying as it pops up every interval. I tried adding a new state to run the code only once:
const [cursorOut, setCursorOut] = useState(false);
React.useEffect(() => {
const popupWhenLeave = (event) => {
if (
event.clientY <= 0 ||
event.clientX <= 0 ||
event.clientX >= window.innerWidth ||
event.clientY >= window.innerHeight
) {
handleClickOpen();
}
};
if(!cursorOut) {
document.addEventListener("mouseleave", popupWhenLeave);
setCursorOut(true);
}
return () => {
document.removeEventListener("mouseleave", popupWhenLeave);
};
}, []);
The above code also ran the same as the previous one i.e. every time the modal would pop up.
I tried to add the dependency to useEffect [cursorOut] but now it won't pop the modal.
I need to pop up the modal only once on the page when the cursor is out and then stop.
P.S. I also tried without useEffect and it doesn't work.