Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
react-router v6 wrapped route component not working

How can you render a composed Route component

code example

Bottomline from above example is that in the following code, the Wrapped route will never render it's element

const App = () => (
      <Routes>
        <Wrapped/>
        <Route path="/inline" element={<span >Inline works</span>} />
      </Routes>
);

const Wrapped = () => <Route path="/wrapped" element={<span>wrapped</span>} />

Is there a way of doing this kind of composition with the Route component with react-router v6? Or will react-router v6 only support Route directly nested in the Routes component?

Edit, more specifically I'm trying to get a recommendation for using a ProtectedRoute component, something among the lines of:

type Props  = {
  element: ReactElement;
  redirectRoute: string;
} & RouteProps;

const ProtectedRoute = ({element, redirectRoute, ...rest}: Props) => {
  const {isAuthenticated} = useAuth();
  <Route {...rest} element={isAuthenticated() ? element : <Navigate to={redirectRoute}/>}/>
}

EDIT: It seems like this used to work in older beta versions, so this might be a bug. At the moment the latest version is 6.0.0-beta.4 & I've logged an issue: https://github.com/remix-run/react-router/issues/8066

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Or will react-router v6 only support Route directly nested in the Routes component?

Correct, RRv6 does not support route composition. Instead, try using your <Wrapped /> component inside the element prop. E.g.

<Route path="/foo" element={<Wrapped>/* something here */</Wrapped>} />
about 4 years ago · Juan Pablo Isaza Denunciar

0

In your code, you are trying to use Wrapped Component as a Router, but it's not. It's a React element returning a React Router element. Since you only need Router in this simple usecase, you can treat is as a function:

<Suspense fallback={null}>
   <Routes>
      {/* Use this as a normal function, and also function name etc.,*/}
      {Wrapped()}
      <Route path="/inline" element={<span>Inline works</span>} />
   </Routes>
</Suspense>

However, I would recommend not to complicate the routes by trying to add customizations on route and instead wrap your component that you want to route.

For eg.,

const Wrapped = () => <Route path="/wrapped" element={<span>wrapped</span>} />;

to

<Route path="/wrapped" element={<Wrapped>wrapped component</Wrapped>} />
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda