Can't make react router work with multi-layer routes with parameters

Ádám Maul

I am trying to make my react-router work. Until now, I only had simple routes like: /login, /logout, /admin. I have a couple of routes below my /admin route like /admin/users, /admin/groups

Now I would like to make it so I can render a component on a route like this: /admin/groups/modify/:groupID. Since the /admin/groups work just fine, I thought I could just write /admin/groups/modify/:groupID, but it doesn't work that way.

So why does /admin/groups work, but not /admin/groups/modify/:groupID? Is it because I have the parameters?

My router looks like this:

<Router>
    <Navbar />
    <Switch className = "container-fluid">
        <Route exact path = "/" component = {Dashboard} />
        <Route exact path = "/login" component = {Login} />
        <Route exact path = "/logout" component = {Logout} />
        <Redirect from = "/register/" to =  "/"/>
        <Route exact path = "/register/:token" component = {Register} />
        <Route exact path = "/groupview" component = {GroupView} />
        <Route exact path = "/admin" component = {AdminDashboard} />
        <Route exact path = "/admin/users" component = {AdminUsers} />
        <Route exact path = "/admin/groups" component = {AdminGroups} />
        <Route exact path = "/admin/groups/modify/:groupID" component = {AdminGroupsModify} />
        <Route exact path = "/admin/regTokens" component = {AdminRegTokens} /> 
    </Switch>
</Router>
mirsahib

You should not put that many exact props into your route component,React route always make partial matching and first come first serve which mean if you declare a path / first and /admin second it will render the first path component first (even if you want the second it won't render that), that's why you should put exact props in your Route component which you need to match perfectly,This is how you should proceed

export default function App() {
  return (
    <>
      <Router>
        <Nav />
        <Switch className="container-fluid">
          <Route exact path="/admin" component={AdminDashboard} />
          <Route path="/admin/regTokens" component={AdminRegTokens} />
          <Route path="/admin/users" component={AdminUsers} />
          <Route exact path="/admin/groups" component={AdminGroups} />
          <Route
            path="/admin/groups/modify/:groupID"
            component={AdminGroupsModify}
          />
        </Switch>
      </Router>
    </>
  );
}

Another way is to put Route which has bigger path first which means putting this Route

<Route path="/admin/groups/modify/:groupID"
       component={AdminGroupsModify}
              />

before this Route

<Route exact path="/admin/groups" component={AdminGroups} />

Reference for better understanding of react route

CodeSandbox Link

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make react router work with static assets, html5 mode, history API and nested routes?

How to make a React multi-step form work with React Router?

React Router v4 routes with parameters not rendering/refreshing

React Router v4 - can't access URL parameters

server fall back and nested routes with react-router and url parameters

Dynamic routes being added to router but don't work

Angular 8 Nested routes and multiple router outlet doesn't work

Can't print child components of nested routes on React Router v5

Can't get layer mask to work properly

React router can't display multiple routes

React Router doesn't work

React Router Routes Don't work when deployed to Heroku

React router nested routes doesn't work

How can I make the 404 route work in React-Router?

Can't make React router dom work, "InvalidCharacterError"

can't make a slug doesn't work in my routes file

Multi Dynamic Routes (react-router-dom)

React router , routes not workin

Can't find description of MaterialApp routes parameters

Can't add Authenticated Routes to react router dom

Why doesn't the new 'Routes' syntax in React-router-dom work on my code?

React router , routes not wrking

React Router v6 Nested Routes doesn't work

How to make a dynamic deeply nested routes with React Router 6?

Nested routes do not work (react router v6)

React router concatenating routes

How to make conditional routes in react-router-dom?

React Router: Stylesheet not working on routes with URL parameters

Can't get React Router to work in my React app