-
I'm trying to use const useBeforeNavigate = (callback: () => void) => {
const blocker = useBlocker(
({ currentLocation, nextLocation }) =>
currentLocation.pathname !== nextLocation.pathname,
);
useEffect(() => {
if (blocker.state === 'blocked') {
callback?.();
blocker.proceed?.();
}
}, [blocker, callback]);
}; It doesn't work for some reason. It allows to go to another page (and saves the data as intended), but after that it blocks the navigaton completely. Is there any way to remove the blocking in useEffect's clean up function? I've tried to do the following: return () => {
if (blocker.state === 'blocked') {
blocker.proceed();
}
} but I get the error: "Can't proceed the blocker which is not in blocked state" |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
Beta Was this translation helpful? Give feedback.
-
I'm facing exactly the same thing |
Beta Was this translation helpful? Give feedback.
I'm not a fan of my solution, but it works for me:
We need to check if we're on the same location the hook was supposed to block, otherwise it blocks all other navigations.
As I said, it looks like a workaround, but I haven't found any other way to solve this.