[Bug]: navigate with unstable_usePrompt, causing Invalid blocker state transition: unblocked -> proceeding
error.
#10489
-
What version of React Router are you using?6.8.0 Steps to ReproduceI created a CodeSandbox playground, which you can access by following the steps below.
Screen.Recording.2023-05-12.at.6.19.31.PM.movExpected BehaviorNo Actual Behavior
ETCIn the linked code sandbox, wrap "navigate" in "setTimeout" seems to work. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 18 replies
-
I am using useBlocker, and I have the same issue. Interesting is, that on version 6.11.2 it is working without error, but on version 6.14.0 is not. |
Beta Was this translation helpful? Give feedback.
-
Does this slightly altered version of function usePrompt({ when, message }: { when: boolean; message: string }) {
let blocker = useBlocker(when);
React.useEffect(() => {
if (blocker.state === "blocked") {
let proceed = window.confirm(message);
if (proceed) {
blocker.proceed();
} else {
blocker.reset();
}
}
}, [blocker, message]);
React.useEffect(() => {
if (blocker.state === "blocked" && !when) {
blocker.reset();
}
}, [blocker, when]);
} |
Beta Was this translation helpful? Give feedback.
-
Thanks for raising this issue. I ran into this yesterday. I had a look at what was going on under the hood and reasoned about this with Matt. I've raised #10687 and explained the cause of the bug there 🙂 |
Beta Was this translation helpful? Give feedback.
-
Per the thread above, this was a bit of an add use-case and can be fixed with slight changes to the application logic. We did merge #10687 which will avoid the thrown exception - that should be released in 6.14.3 since we already started the release branch for 6.14.2 before this was merged. But it's worth noting that this still does not behave as the original issue would have expected. Unblocking and navigating in the same event loop will still trigger the prompt after the fix. There are app-land solutions using |
Beta Was this translation helpful? Give feedback.
-
At version 6.23.1, I can confirm that this issue also can occurs when, between In my case the solution was to disable any URL manipulations during working with |
Beta Was this translation helpful? Give feedback.
Per the thread above, this was a bit of an add use-case and can be fixed with slight changes to the application logic. We did merge #10687 which will avoid the thrown exception - that should be released in 6.14.3 since we already started the release branch for 6.14.2 before this was merged.
But it's worth noting that this still does not behave as the original issue would have expected. Unblocking and navigating in the same event loop will still trigger the prompt after the fix. There are app-land solutions using
flushSync
/useState
/setTimeout
in the threads above.