Maaz Zindani

Loading the index…

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

React Router throws because useNavigation has no data router above it

ErroruseNavigation must be used within a data router.

Short answer

useNavigation, useFetcher, useLoaderData, useRouteError and a few more only work under a data router. BrowserRouter, MemoryRouter and HashRouter are not data routers. Create the router with createBrowserRouter (or createMemoryRouter in tests) and render it with RouterProvider. Plain hooks such as useNavigate keep working either way.

  • Applies to react-router 6.30, React 18, @testing-library/react
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find how the router is created. If you see a JSX router component instead of a create...Router call, that is the cause.

    grep -rnE "BrowserRouter|HashRouter|MemoryRouter|createBrowserRouter|RouterProvider" src
  2. 2Match what you see, then jump to its fix

    1. 1The error appears in the browser as soon as the component with the hook rendersThe app uses BrowserRouter, which is not a data routerFix
    2. 2Only one page needs the hook, and the rest of the app uses nested <Routes> that you do not want to rewriteYou cannot convert every route at onceFix
    3. 3The error appears only in the test run, and the test wraps the component in MemoryRouterA test renders the component without a data routerFix

What does the error look like?

  • useNavigation must be used within a data router.

Cause 1 of 3

The app uses BrowserRouter, which is not a data router

1Check

The React Router docs list createBrowserRouter, createMemoryRouter, createHashRouter and createStaticRouter as data routers. BrowserRouter, MemoryRouter, HashRouter, NativeRouter and StaticRouter are not, and the data hooks (useNavigation among them) need the first group.

grep -rn "<BrowserRouter" src

2Fix

Replace BrowserRouter and Routes with a router object. createRoutesFromElements lets you keep your existing Route elements.

import {
  createBrowserRouter,
  createRoutesFromElements,
  Route,
  RouterProvider,
} from "react-router-dom";

const router = createBrowserRouter(
  createRoutesFromElements(
    <Route path="/" element={<Root />}>
      <Route path="dashboard" element={<Dashboard />} />
    </Route>
  )
);

ReactDOM.createRoot(document.getElementById("root")).render(
  <RouterProvider router={router} />
);

3Verify

Reload the page: the error is gone and the logged state is a string, not a crash.

// inside the component
const navigation = useNavigation();
console.log(navigation.state);

Confirmed by reactrouter.com

Did this fix it?

Cause 2 of 3

You cannot convert every route at once

1Check

Hooks from before 6.4 (useNavigate, useLocation, useParams, Link, Outlet) work in both kinds of app, so only the component using a data hook forces the change.

grep -rnE "useNavigation|useFetcher|useLoaderData|useRouteError|useSubmit" src

2Fix

Migrate in steps: put a data router at the top with one splat route that renders your old Routes tree, then lift the routes that need data hooks into the router, one at a time.

const router = createBrowserRouter([
  { path: "/", element: <Home /> },        // lifted, can use useNavigation
  { path: "*", element: <Root /> },         // old <Routes> tree, unchanged
]);

export default function App() {
  return <RouterProvider router={router} />;
}

3Verify

The lifted route renders its data hooks; the routes still inside Root keep working.

npm start

Confirmed by reactrouter.com

Did this fix it?

Cause 3 of 3

A test renders the component without a data router

1Check

MemoryRouter is not a data router, so a test that wraps the component in it hits the same limit as the app does.

grep -rn "MemoryRouter" src --include=*.test.*

2Fix

Build a memory data router from a route list and render it with RouterProvider.

import { createMemoryRouter, RouterProvider } from "react-router-dom";
import { render, screen } from "@testing-library/react";

const router = createMemoryRouter(
  [{ path: "/", element: <SaveButton /> }],
  { initialEntries: ["/"] }
);
render(<RouterProvider router={router} />);

3Verify

Run the test file.

npx jest SaveButton

Confirmed by reactrouter.com

Did this fix it?

How do I stop it coming back?

  • Pick createBrowserRouter for new web apps; the docs recommend it for all web projects.
  • Keep one router per app and create it outside the React tree, as a module-level constant.
  • Make a small test helper that renders a component inside createMemoryRouter, so tests do not reach for MemoryRouter.

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.