React Router throws that a custom component is not a <Route>
Error: [PrivateRoute] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>Short answer
React Router checks every direct child of <Routes> and accepts only <Route> or a fragment. A custom PrivateRoute placed there fails that check. Keep <Route> as the child and move the login check into its element: either a layout route that renders <Outlet />, or a wrapper component around the page element.
Fix it fast
1Find out which cause you have
Open the file that renders <Routes> and look at the direct children. Every one must be <Route ...>. Any other tag name in the error brackets is the component to move.
grep -rn "<Routes" src/2Match what you see, then jump to its fix
- 1
The name in square brackets is your own component, such as [PrivateRoute] or [AuthRoute].A custom route component is a direct child of <Routes>Fix - 2
No error, but protected pages render blank inside a layout route.The guard wraps the page element but the page never showsFix - 3
The error names a component such as [Redirect] after an upgrade from v5.A <Redirect> or v5 route helper is still inside <Routes>Fix
- 1
What does the error look like?
Error: [PrivateRoute] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>
Cause 1 of 3
A custom route component is a direct child of <Routes>
1Check
In React Router v6 the router reads the <Routes> children as configuration, so it checks the element type of each one. A v5 style wrapper that stands in for <Route> no longer passes.
<Routes>
<PrivateRoute path="/dashboard" element={<Dashboard />} />
</Routes>2Fix
Replace the custom component with a plain <Route>. Put the guard into a layout route that has no path and renders <Outlet />, then nest the protected routes inside it.
import { Navigate, Outlet, Route, Routes } from "react-router-dom";
function RequireAuth() {
const user = useAuth(); // your own auth hook
return user ? <Outlet /> : <Navigate to="/login" replace />;
}
<Routes>
<Route path="/login" element={<Login />} />
<Route element={<RequireAuth />}>
<Route path="/dashboard" element={<Dashboard />} />
</Route>
</Routes>3Verify
Reload the app. The error is gone, a signed-out visit to /dashboard lands on /login, and a signed-in visit shows the page.
npm run devConfirmed by reactrouter.com
Cause 2 of 3
The guard wraps the page element but the page never shows
1Check
A layout route shows its children only where it renders <Outlet />. A guard that returns {children} while it is used as a layout route renders nothing for the nested routes.
grep -rn "children" src/ | grep -i auth2Fix
Return <Outlet /> when the guard is the element of a pathless parent route. Use {children} only when you wrap one page element directly.
// as a parent route
<Route element={<RequireAuth />}>
<Route path="/settings" element={<Settings />} />
</Route>
// or around one element
<Route
path="/settings"
element={<RequireAuth><Settings /></RequireAuth>}
/>3Verify
Open a protected URL while signed in. The page content appears.
npm run devConfirmed by reactrouter.com
Cause 3 of 3
A <Redirect> or v5 route helper is still inside <Routes>
1Check
<Redirect> is not supported as part of the route config in v6.
grep -rn "Redirect" src/2Fix
Render <Navigate> in a route element, or redirect on the server.
<Route path="/old" element={<Navigate to="/new" replace />} />3Verify
Visit /old. The address bar changes to /new.
npm run devConfirmed by reactrouter.com
How do I stop it coming back?
- Keep only <Route> elements (or fragments) as direct children of <Routes>, and put logic in the element.
- Group protected pages under one pathless layout route instead of wrapping each page.
- After a major router upgrade, read the official upgrade guide before changing code.
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