You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

25 lines
814 B

import { useContext, useEffect } from 'react';
// @ts-ignore
import { UNSAFE_NavigationContext as NavigationContext } from 'react-router-dom';
function useBlocker(blocker, when = true) {
const { navigator } = useContext(NavigationContext);
useEffect(() => {
if (!when) return;
// @ts-ignore
const unblock = navigator.block((tx) => {
console.log(tx);
const autoUnblockingTx = Object.assign(Object.assign({}, tx), {
retry() {
// Automatically unblock the transition so it can play all the way
// through before retrying it. TODO: Figure out how to re-enable
// this block if the transition is cancelled for some reason.
unblock();
tx.retry();
},
});
blocker(autoUnblockingTx);
});
return unblock;
}, [navigator, blocker, when]);
}
export default useBlocker;