Skip to content

Instantly share code, notes, and snippets.

@sachinKumarGautam
Created January 18, 2021 11:00
Show Gist options
  • Save sachinKumarGautam/245285add804049a453c859a945b4543 to your computer and use it in GitHub Desktop.
Save sachinKumarGautam/245285add804049a453c859a945b4543 to your computer and use it in GitHub Desktop.
useEffect(() => {
/**
* trapping next router before-pop-state to manipulate router change
* on browser back button quick filter modal should be closed
*/
router.beforePopState(() => {
if (isOpen) {
/**
* only applicable if modal is open.
*/
onClose();
/**
* return false stops next router change but we can not
* prevent browser url change.
* to prevent go to previous url in browser address bar
* we have to use history.forward method.
*/
window.history.forward();
return false;
}
return true;
});
return () => {
router.beforePopState(() => {
return true;
});
};
}, [isOpen]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment