React throws 'You cannot render a <Router> inside another <Router>'
You cannot render a <Router> inside another <Router>.Short answer
React Router throws this when a router component renders somewhere that already sits inside a router. Your app must have exactly one. Search the code for every BrowserRouter, MemoryRouter and Router, keep the one in your entry file, and remove the rest. In tests, wrap the component only if the component has no router of its own.
Fix it fast
1Find out which cause you have
List every place a router component is rendered. More than one hit in the same tree is the bug.
grep -rnE "<(BrowserRouter|MemoryRouter|HashRouter|Router)[ >]" src/2Match what you see, then jump to its fix
- 1
The error appears on first load, before any page showsOne router in the entry file and another in AppFix - 2
The error appears only when you open one page or one widgetA router is rendered inside a component that is already under a routerFix - 3
The error appears in your test runner, not in the browserA test wraps a component in MemoryRouter although the component brings its own routerFix
- 1
What does the error look like?
You cannot render a <Router> inside another <Router>.You should never have more than one in your app.
Cause 1 of 3
One router in the entry file and another in App
1Check
Open the file that calls createRoot and the root App component. A very common layout is a router in both, because one was added by a template and the other by hand.
grep -rn "createRoot" src/
grep -rn "Router" src/index.* src/main.* src/App.*2Fix
Keep a single router around the whole application, in the entry file, and delete the second one. Components below it only use Routes, Route and Link.
// src/main.jsx
import { createRoot } from "react-dom/client";
import { BrowserRouter } from "react-router";
import App from "./App";
createRoot(document.getElementById("root")).render(
<BrowserRouter>
<App />
</BrowserRouter>
);
// src/App.jsx: no router here, only <Routes>3Verify
Reload the page. The error is gone and the routes render.
npm run devConfirmed by reactrouter.com
Cause 2 of 3
A router is rendered inside a component that is already under a router
1Check
The component stack in the console names the component that renders the inner router. The library code rejects any router that is rendered while a router context already exists above it.
grep -rn "Router" src/ --include=*.jsx --include=*.tsx2Fix
Remove the inner router and let that component use the Routes, Route or Link of the router above it. Use an environment router such as BrowserRouter only at the top of the tree.
// before
function Settings() {
return (
<BrowserRouter>
<Routes>...</Routes>
</BrowserRouter>
);
}
// after
function Settings() {
return <Routes>...</Routes>;
}3Verify
Open the page that failed. It renders and navigation inside it works.
npm run devConfirmed by raw.githubusercontent.com
Cause 3 of 3
A test wraps a component in MemoryRouter although the component brings its own router
1Check
MemoryRouter renders a Router under the hood, so wrapping a component that already contains BrowserRouter or MemoryRouter trips the same check.
grep -rn "MemoryRouter" src/ --include=*.test.*2Fix
Render the component that holds the router without a wrapper, or export the routes part separately and wrap only that in MemoryRouter with initialEntries.
render(
<MemoryRouter initialEntries={["/inbox/123"]}>
<AppRoutes />
</MemoryRouter>
);3Verify
Run the test file again.
npm testConfirmed by reactrouter.com
How do I stop it coming back?
- Put the router in exactly one file, the one that mounts the app, and say so in a comment.
- Split App into AppRoutes (no router) and an entry file (router), so tests can wrap AppRoutes.
- Search for 'Router' in a pull request that adds a new page or a library.
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