Maaz Zindani

Loading the index…

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

useRoutes() fails because no Router wraps the component

ErrorError: useRoutes() may be used only in the context of a <Router> component.

Short answer

useRoutes reads the location that a router provides to the components below it. The error means the component calling it sits above the router or outside any router. Move BrowserRouter up to where React is mounted, so App renders inside it, and give tests a MemoryRouter.

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

Fix it fast

  1. 1Find out which cause you have

    Find the component that calls useRoutes and the place where the router is created. The router must be an ancestor of the caller, not a sibling or a child.

    grep -rn "useRoutes\|BrowserRouter\|createBrowserRouter" src
  2. 2Match what you see, then jump to its fix

    1. 1One file contains both <BrowserRouter> and a useRoutes() call, and the hook sits above the JSX that renders the routerApp calls useRoutes and renders the Router in the same componentFix
    2. 2The app works in the browser but the same error appears in a unit testA test renders the component without any routerFix

What does the error look like?

  • Error: useRoutes() may be used only in the context of a <Router> component.

Cause 1 of 2

App calls useRoutes and renders the Router in the same component

1Check

A router provides location context to everything rendered inside it. Hooks that run in the same component body run before that component has rendered anything, so no router exists above them yet.

grep -n -E 'BrowserRouter|useRoutes' src/App.jsx src/main.jsx src/index.js

2Fix

Wrap App in the router where you call createRoot, and keep useRoutes inside App.

// 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>
);

// App.jsx
import { useRoutes } from "react-router";
export default function App() {
  return useRoutes(routes);
}

3Verify

Start the dev server and open a route; the app renders and the error is gone.

npm run dev

Confirmed by reactrouter.com

Did this fix it?

Cause 2 of 2

A test renders the component without any router

1Check

In a test there is no main entry file, so nothing wraps the component. The React Router docs name MemoryRouter and createMemoryRouter as the routers that suit tests, because they do not need the browser history API.

grep -rn "render(" src --include=*.test.*

2Fix

Wrap the component under test in a MemoryRouter, with the starting URL you want.

import { render } from "@testing-library/react";
import { MemoryRouter } from "react-router";

render(
  <MemoryRouter initialEntries={["/dashboard"]}>
    <App />
  </MemoryRouter>
);

3Verify

Run the test file.

npx vitest run src/App.test.jsx

Confirmed by reactrouter.com

Did this fix it?

How do I stop it coming back?

  • Pick one router style per app, as the React Router docs advise, and mount it once where React is mounted.
  • Create a small test helper that wraps components in MemoryRouter, so each test does not repeat it.

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.