Warn users before they leave a React Router page with unsaved changes
Prompt or usePrompt stops working after upgrading from React Router v5Short answer
Use useBlocker instead of Prompt. It needs a data router such as createBrowserRouter, so it does nothing under a plain BrowserRouter. It blocks only in-app navigation: for a tab close or a page reload, add your own beforeunload handler. Call blocker.proceed() or blocker.reset() to answer the user's choice.
Fix it fast
1Find out which cause you have
Find out which router your app mounts. useBlocker only works with a data router; a plain BrowserRouter component is not one.
grep -rn "BrowserRouter\|createBrowserRouter\|RouterProvider" src/2Match what you see, then jump to its fix
- 1
Import of Prompt or usePrompt fails, or it is flagged as unstableThe app still uses the v5 Prompt componentFix - 2
useBlocker compiles but never blocksThe router is not a data routerFix - 3
The in-app links are blocked, but closing the tab or pressing reload loses the dataA tab close or page reload is not blockedFix
- 1
What does the error look like?
Prompt or usePrompt stops working after upgrading from React Router v5useBlocker is called but navigation is never blocked
Cause 1 of 3
The app still uses the v5 Prompt component
1Check
The v6.30 documentation for useBlocker is the documented way to stop a navigation. The only prompt hook in the v6.30 sidebar is named unstable_usePrompt, so do not build on it.
grep -rn "Prompt" src/ --include=*.tsx --include=*.jsx2Fix
Replace Prompt with useBlocker. Pass a boolean or a function that decides when to block, and render your own confirm UI from the blocker state.
import { useBlocker } from "react-router-dom";
function EditForm({ dirty }) {
const blocker = useBlocker(dirty);
return blocker.state === "blocked" ? (
<div>
<p>Leave without saving?</p>
<button onClick={() => blocker.proceed()}>Leave</button>
<button onClick={() => blocker.reset()}>Stay</button>
</div>
) : null;
}3Verify
Type into the form, click a link to another route, and confirm the dialog appears.
npm run devConfirmed by reactrouter.com
Cause 2 of 3
The router is not a data router
1Check
The React Router documentation says useBlocker only works with a data router, such as createBrowserRouter. A BrowserRouter wrapper component does not qualify.
grep -rn "<BrowserRouter" src/2Fix
Create the router with createBrowserRouter and mount it with RouterProvider.
import { createBrowserRouter, RouterProvider } from "react-router-dom";
const router = createBrowserRouter([
{ path: "/", element: <Layout />, children: [/* routes */] },
]);
root.render(<RouterProvider router={router} />);3Verify
Repeat the in-app navigation test. The blocker state should change to blocked.
npm run devConfirmed by reactrouter.com
Cause 3 of 3
A tab close or page reload is not blocked
1Check
The documentation says useBlocker does not handle hard reloads or cross-origin navigations; it only sees client-side navigation.
grep -rn "beforeunload" src/2Fix
Keep useBlocker for in-app links and add a beforeunload listener while the form is dirty.
useEffect(() => {
if (!dirty) return;
const handler = (e) => e.preventDefault();
window.addEventListener("beforeunload", handler);
return () => window.removeEventListener("beforeunload", handler);
}, [dirty]);3Verify
Edit the form and reload the page. The browser should ask before leaving.
npm run devConfirmed by reactrouter.com
How do I stop it coming back?
- Mount the router with createBrowserRouter and RouterProvider from the start.
- Block only when the form is dirty, and compare pathnames so a change of query string does not trigger the dialog.
- Always handle the blocked state: call proceed() or reset(), or the user is stuck.
- The docs call blocking navigation a bit of an anti-pattern; consider saving the draft to sessionStorage instead.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix