React Router throws: A Route is only ever to be used as the child of Routes
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>.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.
Fix it fast
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 402Match what you see, then jump to its fix
- 1
Error says the Route is never to be rendered directlyA Route is rendered outside any RoutesFix - 2
The message names your component: [PrivateRoute] is not a <Route> componentYour own component wraps the Route (PrivateRoute style)Fix - 3
The same 'is not a <Route> component' message names a div or another tagRoutes are grouped with a wrapper element instead of a FragmentFix
- 1
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 devConfirmed by reactrouter.com
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 devConfirmed by raw.githubusercontent.com
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 devConfirmed by raw.githubusercontent.com
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:
- 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
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 fixWeb 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 fix