Maaz Zindani

Loading the index…

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

React throws 'You cannot render a <Router> inside another <Router>'

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

  • Applies to React 19, React Router 7, react-router 7 declarative mode
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1The error appears on first load, before any page showsOne router in the entry file and another in AppFix
    2. 2The error appears only when you open one page or one widgetA router is rendered inside a component that is already under a routerFix
    3. 3The error appears in your test runner, not in the browserA test wraps a component in MemoryRouter although the component brings its own routerFix

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 dev

Confirmed by reactrouter.com

Did this fix it?

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=*.tsx

2Fix

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 dev

Confirmed by raw.githubusercontent.com

Did this fix it?

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 test

Confirmed by reactrouter.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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