Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!