2021-04-19 17:36:58 +02:00
|
|
|
// This is unsafe and can be not chained as `popstate` event is asynchronous
|
|
|
|
|
2021-06-14 21:12:34 +02:00
|
|
|
export default function useHistoryBack(handler: NoneToVoidFunction) {
|
|
|
|
function handlePopState() {
|
|
|
|
handler();
|
2021-06-12 16:20:25 +02:00
|
|
|
}
|
2021-04-19 17:36:58 +02:00
|
|
|
|
2021-06-14 21:12:34 +02:00
|
|
|
window.addEventListener('popstate', handlePopState);
|
|
|
|
window.history.pushState({}, '');
|
2021-06-12 16:20:25 +02:00
|
|
|
|
2021-06-14 21:12:34 +02:00
|
|
|
return () => {
|
|
|
|
window.removeEventListener('popstate', handlePopState);
|
|
|
|
window.history.back();
|
2021-04-19 17:36:58 +02:00
|
|
|
};
|
|
|
|
}
|