Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Warn users before they leave a React Router page with unsaved changes

ErrorPrompt or usePrompt stops working after upgrading from React Router v5

Short 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.

  • Applies to React Router 6.30, React Router 7, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1Import of Prompt or usePrompt fails, or it is flagged as unstableThe app still uses the v5 Prompt componentFix
    2. 2useBlocker compiles but never blocksThe router is not a data routerFix
    3. 3The in-app links are blocked, but closing the tab or pressing reload loses the dataA tab close or page reload is not blockedFix

What does the error look like?

  • Prompt or usePrompt stops working after upgrading from React Router v5
  • useBlocker 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=*.jsx

2Fix

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 dev

Confirmed by reactrouter.com

Did this fix it?

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 dev

Confirmed by reactrouter.com

Did this fix it?

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 dev

Confirmed by reactrouter.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.