Replies: 1 comment 1 reply
-
The blocker function receives the current/next locations as arguments, so as a workaround, could you add a query string to your redirect location and then use // Assuming you redirect to /dashboard?success=true
let shouldBlock = React.useCallback(({ nextLocation }) => {
return isDirty && nextLocation.search !== "?success=true";
}, [isDirty]);
let blocker = unstable_useBlocker(shouldBlock); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi there
I'm loving the
unstable_useBlocker
API. I've built a reusableBlocker
component which uses this hook and shows a modal when open. I'm currently using it in a large form, with the block state being based on React Hook Form'sisDirty
prop. Additionally, I'm using Tanstack Query to trigger a mutation on submit, which redirects on success.Now the issue: the
Blocker
blocks the redirect.The blocker seems to unblock in the next render cycle (e.g. when using React Hook Form's
reset
to un-dirty the form), and there's no real way to circumvent this. The best solution as of now is to accept this behavior and redirect in the next render cycle with auseEffect
.As blockers are usually shown when the user leaves a page, I think it would make sense for the developer to have controller whether they want to use the blocker as well. Something like
navigate('/dashboard', { blockable: false })
, which would then get ignored by the blocker.Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions