Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - react-router-dom two Route conflict

Here is my two Route and when I am navigate to /blogs/new BlogsShow Component also running. How to prevent that.

<Route path="/blogs/new" component={BlogsNew} />
<Route path="/blogs/:id" component={BlogsShow} />
like image 819
SaraVanaN Avatar asked Oct 07 '17 14:10

SaraVanaN


1 Answers

Wrap those two routes in a <Switch> component and the first one that matches will render exclusively.

For example something like this:

import { Switch, Route } from 'react-router-dom';

// ...code

<Switch>
    <Route path="/blogs/new" component={BlogsNew} />
    <Route path="/blogs/:id" component={BlogsShow} />
</Switch>

For more details see their documentation.

like image 170
Bill Avatar answered Oct 15 '22 22:10

Bill