element-call/src/useLocationNavigation.ts
Šimon Brandner 3b74920ece
useLocationNavigation
Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
2022-07-30 10:00:34 +02:00

25 lines
560 B
TypeScript

import { useEffect } from "react";
import { useHistory } from "react-router-dom";
export function useLocationNavigation(enabled = false): void {
const history = useHistory();
useEffect(() => {
let unblock;
if (enabled) {
unblock = history.block((tx) => {
const url = new URL(tx.pathname, window.location.href);
url.search = tx.search;
url.hash = tx.hash;
window.location.href = url.href;
});
}
return () => {
if (unblock) {
unblock();
}
};
}, [history, enabled]);
}