Maaz Zindani

Loading the index…

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

React Router throws that a custom component is not a <Route>

ErrorError: [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.

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

Fix it fast

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

    1. 1The 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. 2No error, but protected pages render blank inside a layout route.The guard wraps the page element but the page never showsFix
    3. 3The error names a component such as [Redirect] after an upgrade from v5.A <Redirect> or v5 route helper is still inside <Routes>Fix

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 dev

Confirmed by reactrouter.com

Did this fix it?

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 auth

2Fix

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 dev

Confirmed by reactrouter.com

Did this fix it?

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 dev

Confirmed by reactrouter.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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