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

90
Vistas
Is this a good practice?

I`m doing a project with react and I decided to make routes functionality a little different, I put the routes details to an separated file as an array: routes details file, and on the routes component I iterate through the array using the map function: routes component. But I don't know if what I did is something recommended, do you think it's better to put the routes in a static way directly in the component or is there a better solution?

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

0

It is better to use the useRoutes function provided by the react-router-dom package.

This makes your code shorter and authentication can be added easily using this procedure.

This way, you can define the main routes as an object like:

routes = (loggedIn) => [
    {
        path: '/',
        children: [
            // unprotected routes here
        ]
    },
    {
        path: '/main',
        element: loggedIn ? <Layout /> : <Navigate to='/login' />
        children: [
            // protected routes here
        ]
    }
]

Then return useRoutes in the route component like:

const { loggedIn } = useSelector((state) => state.auth);
return useRoutes(routes(loggedIn), '');

useRoutes API: https://reactrouter.com/docs/en/v6/api#useroutes

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