Skip to content
Advertisement

React router v6, group routes by feature

I am using React router v6.

THE PROBLEM:

my App.tsx is very big as it includes routes for all of the application:

Ex.

JavaScript

I would like to group these routes by feature so that I end up having something like this:

JavaScript

it would look cleaner and easier to read.

So far I have created something like this for the Admin section:

JavaScript

and I have imported it like this inside App.tsx:

JavaScript

I am expecting to see the <Admin /> component (defined in AdminRoutes), although I don’t get any errors the screen is blank.

Advertisement

Answer

JavaScript

Since you’re using relative paths, the actual url that this will match is /admin/admin, one comes from the top level route in App, and another from here. Assuming you wanted this to only match “/admin”, you can instead do:

JavaScript

Or you could use an absolute path:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement