Pons Suarez
Pons Suarez

Reputation: 83

React Router doesn't match path

enter image description here

This is my routing configuration, when i access the index route which is "/" it works perfectly fine, but when i access the /posts and /response route, it cant resolve it.

Index route

See that is my index route. and this happens when i go to posts or response route enter image description here

Upvotes: 1

Views: 2307

Answers (2)

Andres Mateo Otalvaro
Andres Mateo Otalvaro

Reputation: 141

Ok, first of all, it seems like you're using an unnecessary Router component, you can check here why: https://reacttraining.com/react-router/web/api/Router

Also, you can wrap your routes inside a Switch component, but you will have to change the order of them since a Switch only renders the first route that matches the path given. You can read more about that here https://reacttraining.com/react-router/web/api/Switch. So your routes definition will become something like this:

<Switch>
    <Route path="/r1" render={() => <h1>route 1</h1>} />
    <Route path="/r2" render={() => <h1>route 2</h1>} />
    <Route exact path="/" component={SomeComponent} />
 </Switch>

Upvotes: 3

Davi DeBarros
Davi DeBarros

Reputation: 368

try wrapping your all 3 Route inside a Switch statement

Upvotes: 1

Related Questions