useBlocker on v7 Framework not working
#14776
Replies: 1 comment
-
|
@George-Madeley the issue is almost certainly a stale closure. fix: extract the dirty state into react state and wrap in const { formState: { isDirty } } = form;
const shouldBlock = useCallback<BlockerFunction>(
({ currentLocation, nextLocation }) =>
isDirty && currentLocation.pathname !== nextLocation.pathname,
[isDirty]
);
const blocker = useBlocker(shouldBlock);the pathname check prevents blocking same-page navigations (hash changes, search params). and make sure you're navigating via the navigation blocking guide and the design decision doc cover the edge cases in detail. also note: only one blocker can be active at a time per the current implementation. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a React Router v7 Framework app (with SSR disabled). I have a react-hook-form Form on a given page. Ideally, I want to block the user from navigating away when any part of the form is dirty. I thought the solution below would be simple enough:
But for some reason, the blocker never blocks. I've also tried
useBeforeUnload()hook and the actualbeforeUnloadwindow event but they also do not work. I understand theuseBlockeris used for internal navigation with<Link />andnavigate()whilstbeforeUnloadif for page reloads, navigate backwards and forwards etc., So surely the solution above should work?This did work on v6 Data Router before migrating to v7 Framework. I thought that the reason would be because
useBlockeris not available on the framework router but the docs just state that the:I thought that by setting
ssr: false,in thereact-router.config.tsfile would solve this as:But this does not seem to be the case.
Has anyone else had any issues with
useBlockerin React Router v7 Framework SPA? If so, how did you resolve the issue?Beta Was this translation helpful? Give feedback.
All reactions