Created
May 25, 2022 10:42
-
-
Save nitaking/e9fcfe422a28e3fcf8af5ceb54af07f5 to your computer and use it in GitHub Desktop.
ブラウザバックしたときに処理を実行したいときに使用するhooks
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
import { useEffect } from 'react'; | |
/** | |
* ブラウザバックしたときに処理を実行したいときに使用するhooks | |
*/ | |
export const useOnBrowserBack = (onBrowserBack: () => void) => { | |
useEffect(() => { | |
window.history.pushState(null, '', window.location.pathname); | |
window.addEventListener('popstate', onBrowserBack); | |
return () => { | |
window.removeEventListener('popstate', onBrowserBack); | |
}; | |
}, [onBrowserBack]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment