Next.js Discord

Discord Forum

NextJS 13 app router listener for changes

Unanswered
Blood cockle posted this in #help-forum
Open in Discord
Blood cockleOP
I'm currently working on an app router and need to verify when the user is trying to leave the page. I understand that there's currently no direct way to do this within the useRouter from 'next/navigation'. But I wanted to know what alternatives do we have for achieving this behavior.

3 Replies

onbeforeunload
Blood cockleOP
hello @DirtyCajunRice | AppDir can you provide an example?
Blood cockleOP
I'm doing:

useEffect(() => {
window.onbeforeunload = () => true;

return () => {
window.onbeforeunload = null;
};
}, []);

But this is only working when I try to reload, not when I'm trying to navigate to another part of my site.