Created
March 27, 2019 16:47
-
-
Save fabe/f247cb654d185b34ae16efa857864b67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getUserConfirmation = (location, callback) => { | |
const [newUrl, action] = location.split('|'); | |
// Check if user wants to navigate to the same page. | |
// If so, we don't want to trigger the page transitions. | |
// We have to check the `action`, because the pathnames | |
// are the same when going back in history 🤷 | |
const currentUrl = `${window.location.pathname}${window.location.search}`; | |
if (newUrl === currentUrl && action === 'PUSH') { | |
callback(false); | |
return; | |
} | |
callback(true); | |
}; | |
let history = createHistory({ getUserConfirmation }); | |
// `block` must return a string to conform. | |
// We send both the pathname and action to `getUserConfirmation`. | |
history.block((location, action) => { | |
return `${location.pathname}${location.search}|${action}`; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment