React Router throws because useNavigation has no data router above it
useNavigation 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.
Fix it fast
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" src2Match what you see, then jump to its fix
- 1
The error appears in the browser as soon as the component with the hook rendersThe app uses BrowserRouter, which is not a data routerFix - 2
Only 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
The error appears only in the test run, and the test wraps the component in MemoryRouterA test renders the component without a data routerFix
- 1
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" src2Fix
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
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" src2Fix
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 startConfirmed by reactrouter.com
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 SaveButtonConfirmed by reactrouter.com
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:
- 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
useNavigate and Link throw because no router is above them
Cannot destructure property 'basename' of 'React2.useContext(...)' as it is null
Router hooks and Link read the router from React context.
2 causes, each with a fixWeb 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 fix