Maaz Zindani

Loading the index…

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

React Router throws: A Route is only ever to be used as the child of Routes

ErrorError: A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Please wrap your <Route> in a <Routes>.

Short answer

React Router reads Route elements as configuration only when they sit directly inside Routes. If a Route renders on its own, or is hidden inside your own wrapper component, the app throws this error. Wrap the Route in Routes, and replace custom wrapper routes with a layout route or an element wrapper.

  • 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 every Route in the project and look at its parent. The parent must be Routes (or a Fragment inside Routes).

    grep -rn "<Route" src/ | head -n 40
  2. 2Match what you see, then jump to its fix

    1. 1Error says the Route is never to be rendered directlyA Route is rendered outside any RoutesFix
    2. 2The message names your component: [PrivateRoute] is not a <Route> componentYour own component wraps the Route (PrivateRoute style)Fix
    3. 3The same 'is not a <Route> component' message names a div or another tagRoutes are grouped with a wrapper element instead of a FragmentFix

What does the error look like?

  • Error: A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Please wrap your <Route> in a <Routes>.
  • [PrivateRoute] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>

Cause 1 of 3

A Route is rendered outside any Routes

1Check

A Route that renders by itself, for example inside a normal component or inside an old v5 Switch, throws in v6 and v7, as the library source shows.

grep -rn "Switch" src/
grep -rn "<Route" src/

2Fix

Put the Route inside Routes. Routes picks the best match for the current URL and renders that branch.

import { Routes, Route } from 'react-router-dom';

<Routes>
  <Route path="/" element={<Home />} />
  <Route path="about" element={<About />} />
</Routes>

3Verify

Reload the page. The route renders and the console shows no error.

npm run dev

Confirmed by reactrouter.com

Did this fix it?

Cause 2 of 3

Your own component wraps the Route (PrivateRoute style)

1Check

Routes builds its list by looking at its direct children. It accepts Route and React.Fragment only. A custom component that returns a Route is rejected, as the library source shows.

grep -rn "PrivateRoute\|ProtectedRoute" src/

2Fix

Make the guard a normal component and use it as the element of a Route, or as a layout route that renders an Outlet.

import { Navigate, Outlet } from 'react-router-dom';

function RequireAuth({ signedIn }) {
  return signedIn ? <Outlet /> : <Navigate to="/login" replace />;
}

<Routes>
  <Route path="login" element={<Login />} />
  <Route element={<RequireAuth signedIn={signedIn} />}>
    <Route path="account" element={<Account />} />
  </Route>
</Routes>

3Verify

Open /account signed out. You land on /login, with no error.

npm run dev

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

Routes are grouped with a wrapper element instead of a Fragment

1Check

Only Route and React.Fragment pass the child check. A div, or a component that returns several Routes, fails it.

grep -rn -B2 "<Route" src/ | grep -n "<div\|<Suspense"

2Fix

Replace the wrapper with a Fragment, or move the Suspense or layout markup into the element prop of a parent Route.

<Routes>
  <>
    <Route path="a" element={<A />} />
    <Route path="b" element={<B />} />
  </>
</Routes>

3Verify

Both paths render after a reload.

npm run dev

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Keep route definitions in one file, as direct children of Routes, with components as the element prop.
  • Write guards as layout routes with Outlet or Navigate, not as Route replacements.
  • When upgrading from v5, search for Switch and custom Route components first.

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

Web apps

React Router shows a blank page and warns about a leaf route

Matched leaf route at location "/" does not have an element or Component. This means it will render an <Outlet /> with a null value by default resulting in an "empty" page.

React Router prints this warning in development when the deepest matched route has no element, Component or lazy.

3 causes, each with a fix

Next step

Want this handled for good?

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