Warning on refresh (app directory)
Answered
Cordilleran Flycatcher posted this in #help-forum
Cordilleran FlycatcherOP
Hey guys, whenever a user refreshes my Next.js app or tries to go to a different page/website, I want an popup message to appear warning them that they will lose their data/progress. I have tried using the beforeunload event listener with a useEffect hook but it didn't work. Here are some examples of what I've tried -
#1
#2
I am using the app directory. Anyone know how I can accomplish this? Thanks
EDIT - I get the popup when my inspect element is open
#1
useEffect(() => {
window.beforeunload = function() {
return true;
};
return () => {
window.beforeunload = null;
};
}, []);#2
useEffect(() => {
const unloadCallback = (event) => {
event.preventDefault();
event.returnValue = "";
return "";
};
window.addEventListener("beforeunload", unloadCallback);
return () => window.removeEventListener("beforeunload", unloadCallback);
}, []);I am using the app directory. Anyone know how I can accomplish this? Thanks
EDIT - I get the popup when my inspect element is open
Answered by Cordilleran Flycatcher
EDIT - SOLVED! If you haven't interacted with the website at all, there is nothing to lose when you refresh. only when I do shit does the popup come. That makes a lot of sense.
2 Replies
Cordilleran FlycatcherOP
I am getting this error
Cordilleran FlycatcherOP
EDIT - SOLVED! If you haven't interacted with the website at all, there is nothing to lose when you refresh. only when I do shit does the popup come. That makes a lot of sense.
Answer