I am trying to open a popup while a user closes a tab in react using this code:
useEffect(() => {
window.addEventListener('beforeunload',handleUnload );
return () => {
window.removeEventListener('beforeunload', handleUnload);
}
}, []);
const handleUnload = (e) => {
e.preventDefault();
console.log('hey')
alert("HEY");
}
is there any other way to do this or is there any way I can correct the code. Currently I'm trying to alert the user.