Skip to content

Instantly share code, notes, and snippets.

@nitaking
Created May 25, 2022 10:42
Show Gist options
  • Save nitaking/e9fcfe422a28e3fcf8af5ceb54af07f5 to your computer and use it in GitHub Desktop.
Save nitaking/e9fcfe422a28e3fcf8af5ceb54af07f5 to your computer and use it in GitHub Desktop.
ブラウザバックしたときに処理を実行したいときに使用するhooks
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