telegram-tt/src/hooks/useHistoryBack.ts

16 lines
401 B
TypeScript
Raw Normal View History

2021-04-19 17:36:58 +02:00
// This is unsafe and can be not chained as `popstate` event is asynchronous
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
window.addEventListener('popstate', handlePopState);
window.history.pushState({}, '');
2021-06-12 16:20:25 +02:00
return () => {
window.removeEventListener('popstate', handlePopState);
window.history.back();
2021-04-19 17:36:58 +02:00
};
}