Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

93
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda