How to Fix the React Router useNavigate Error
If a component that calls useNavigate() from React Router is not placed inside <BrowserRouter> or <RouterProvider>, the console shows the error useNavigate() may be used only in the context of a <Router> component. and the page turns blank. useNavigate() is the hook for moving between pages from code (for example, going to a list page when a button is pressed, or returning to the top page after logging in). Moving the component inside a Router, or moving the Router further out, makes the error go away. If you import from both react-router-dom and react-router, aligning all imports to react-router makes it go away too.
In my test environment, I reproduced the three cases where this error appears, plus the case where it appears in tests, and confirmed that the fixes remove the error.
The test environment was Ubuntu 24.04 with Node.js 22.23.4. I created a project with the react-ts template of npm create vite@latest (React 19.3.1, Vite 8.3.8, TypeScript 7.0.2), added React Router 8.5.1, and wrapped the components in StrictMode. For the mixed-import check I used react-router-dom 7.18.5, and for the tests I used Vitest 5.1.0, jsdom 31.0.0, and Testing Library 16.3.3. I opened the pages in Chromium 153 operated by Playwright 1.63.0, and I took the DevTools screenshots with a headed Chromium running on a virtual display (Xvfb). I did not test React Router's framework mode, server rendering, Jest, or browsers other than Chromium.
- 1. The full error message in the console
- 2. App returns BrowserRouter and calls useNavigate in the same component
- 3. Header placed next to RouterProvider
- 4. Mixing react-router-dom and react-router imports
- 5. In tests, wrap the component in MemoryRouter
- 6. Type checks miss it, and the production build shows the same message
The full error message in the console
When I opened a page that called useNavigate() outside a Router in the Vite dev server, the console showed the following error.
Uncaught Error: useNavigate() may be used only in the context of a <Router> component.
A warning containing the name of the component where the error happened also follows.
An error occurred in the <Header> component.
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://react.dev/link/error-boundaries to learn more about error boundaries.

The error message means that useNavigate() can only be used inside <Router>. In DevTools, the line at Header (Header.tsx:4:20) appears below the error, which shows the component that called useNavigate() and the line in the file. The page was blank, and the <div id="root"> where the components are mounted was empty.
In the React Router source, the check run from useNavigate (useNavigateUnstable) only looks at whether a Router exists among the ancestors (it checks for the context that the Router provides). If there is no Router, it throws this error. Both <BrowserRouter> and <RouterProvider> count as a Router, so the same message appears whichever one you use.
App returns BrowserRouter and calls useNavigate in the same component
The most common cause is writing useNavigate() directly in a component that returns <BrowserRouter>.
import { BrowserRouter, Route, Routes, useNavigate } from 'react-router';
// Wrong: useNavigate is called in a component that returns BrowserRouter
export default function BadApp() {
const navigate = useNavigate();
return (
<BrowserRouter>
<button onClick={() => navigate('/cats')}>猫の一覧へ</button>
<Routes>
<Route path="/" element={<p>トップ</p>} />
<Route path="/cats" element={<p>猫の一覧</p>} />
</Routes>
</BrowserRouter>
);
}
The button looks like it is inside <BrowserRouter>, but useNavigate() is called by BadApp itself, and BadApp is the parent of <BrowserRouter>. In my test, the error occurred as soon as the page opened, and the warning said An error occurred in the <BadApp> component.
To fix this, move <BrowserRouter> into main.tsx and wrap the App from the outside. The official documentation's example of the modes also uses this structure. My test main.tsx switched what it rendered based on the page URL, but in this case it renders the same as the following code.
// main.tsx
createRoot(document.getElementById('root')!).render(
<StrictMode>
<BrowserRouter>
<GoodApp />
</BrowserRouter>
</StrictMode>,
)
import { Route, Routes, useNavigate } from 'react-router';
// Fixed: BrowserRouter is placed outside in main.tsx, and the App only uses useNavigate
export default function GoodApp() {
const navigate = useNavigate();
return (
<>
<button onClick={() => navigate('/cats')}>猫の一覧へ</button>
<Routes>
<Route path="/" element={<p>トップ</p>} />
<Route path="/cats" element={<p>猫の一覧</p>} />
</Routes>
</>
);
}
On the fixed page, the error did not appear and "Top" was shown. When I pressed the button, the URL path changed to /cats and the display changed to "Cat list".
Header placed next to RouterProvider
In data mode, which uses createBrowserRouter and <RouterProvider>, placing a header shown on every page beside <RouterProvider> causes this error. The following Header is a component that only has a button to go to the list page.
import { useNavigate } from 'react-router';
export default function Header() {
const navigate = useNavigate();
return (
<header>
<button onClick={() => navigate('/cats')}>猫の一覧へ</button>
</header>
);
}
import { createBrowserRouter } from 'react-router';
import { RouterProvider } from 'react-router/dom';
import Header from './Header';
const router = createBrowserRouter([
{ path: '/', element: <p>トップ</p> },
{ path: '/cats', element: <p>猫の一覧</p> },
]);
// Wrong: Header is placed outside (beside) RouterProvider
export default function DataBad() {
return (
<>
<Header />
<RouterProvider router={router} />
</>
);
}
Header is beside <RouterProvider>, not inside it, so it is outside the Router. The screenshot above is of this page being opened.
In data mode, only the elements written in the routes of createBrowserRouter are inside <RouterProvider>. Things like a header that appears on every page should go into a parent route without a path (a layout route in the official documentation). Each page is displayed at the position of the <Outlet /> placed in the parent route's component.
import { Outlet, createBrowserRouter } from 'react-router';
import { RouterProvider } from 'react-router/dom';
import Header from './Header';
// Fixed: Header is placed in a layout route that wraps all pages
function Layout() {
return (
<>
<Header />
<Outlet />
</>
);
}
const router = createBrowserRouter([
{
element: <Layout />,
children: [
{ path: '/', element: <p>トップ</p> },
{ path: '/cats', element: <p>猫の一覧</p> },
],
},
]);
export default function DataGood() {
return <RouterProvider router={router} />;
}
On the fixed page, "Top" appeared below the header's button, and no error appeared. When I pressed the button, the URL path became /cats and the display changed to "Cat list".

Mixing react-router-dom and react-router imports
Sometimes this error appears even when the component is inside a Router. In my test, it appeared when I imported <BrowserRouter> from react-router-dom and useNavigate from react-router.
import { BrowserRouter } from 'react-router-dom';
import Header from './Header';
// Wrong: BrowserRouter is imported from react-router-dom, while useNavigate (inside Header) is imported from react-router
export default function MixedBad() {
return (
<BrowserRouter>
<Header />
</BrowserRouter>
);
}
In this case, React Router 8.5.1 and react-router-dom 7.18.5 were both installed in the project. Running npm ls react-router showed another, different version of react-router installed under react-router-dom (the paths below are replaced with example values).
app@0.0.0 /path/to/app
+-- react-router-dom@7.18.5
| `-- react-router@7.18.5
`-- react-router@8.5.1
When two copies of React Router are installed, the Router information that <BrowserRouter> provides and the Router information that useNavigate() looks for (a React context) become separate objects. As a result, useNavigate() sees no Router above it. In the files Vite pre-bundled, the code of react-router 8.5.1 and the code of react-router 7.18.5 under react-router-dom were in different files. The React Router source also has a developer TODO at this error in useNavigate, with a comment saying that this error is probably caused by two versions of React Router being loaded for some reason.
In React Router 8, the react-router-dom package no longer exists. The official v7 upgrade guide says to run npm uninstall react-router-dom and replace the imports with react-router. DOM-specific items such as RouterProvider are imported from react-router/dom. In my test, I aligned the BrowserRouter import to react-router. The error stopped appearing, and when I pressed the button, the URL path changed to /cats.
In tests, wrap the component in MemoryRouter
Even if the app works correctly, rendering a component that uses useNavigate() alone in a test causes the same error. I rendered Header in two ways with Vitest and Testing Library.
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router';
import { expect, test } from 'vitest';
import Header from '../cases/Header';
// Wrong: rendered without a Router wrapper
test('Router で包まずに描画する', () => {
render(<Header />);
expect(screen.getByRole('button', { name: '猫の一覧へ' })).toBeTruthy();
});
// Fixed: rendered wrapped in MemoryRouter
test('MemoryRouter で包んで描画する', () => {
render(
<MemoryRouter>
<Header />
</MemoryRouter>,
);
expect(screen.getByRole('button', { name: '猫の一覧へ' })).toBeTruthy();
});
When I ran npx vitest run, only the test without a wrapper failed (excerpt).
FAIL src/test/Header.test.tsx > Router で包まずに描画する
Error: useNavigate() may be used only in the context of a <Router> component.
❯ invariant node_modules/react-router/dist/development/lib/router/history.js:189:79
❯ useNavigateUnstable node_modules/react-router/dist/development/lib/hooks.js:272:2
❯ useNavigate node_modules/react-router/dist/development/lib/hooks.js:269:72
❯ Header src/cases/Header.tsx:4:20
Tests 1 failed | 1 passed (2)
MemoryRouter is a Router that keeps its navigation history in memory instead of in the browser. In my test, wrapping Header with it alone was enough to remove the error.
Type checks miss it, and the production build shows the same message
All of the mistakes above are valid types, so npx tsc -p tsconfig.app.json --noEmit did not report an error. The oxlint tool included in the template (npx oxlint src) also reported nothing. You only notice the problem when you open the page in a browser or render the component in a test.
When I built for production with npx vite build and opened the result, the error message was the same as in the dev server: useNavigate() may be used only in the context of a <Router> component. However, the function names in the stack were shortened to names like C, and the warning with the component name (An error occurred in the <…> component.) did not appear, so I could not tell which component caused it. If this happens in production, open the same page in the dev server and check the component name and file line shown in DevTools.




