Muzammil Bilwani
Muzammil Bilwani

Reputation: 146

Redirect '/' to a specific route in react-router 4 at app start

I am trying to redirect my application to a specific route at starting of app. I tried this

 <Route exact path="/todos" component={Todo} />
 <Route exact path="/users" component={User} />
 <Redirect path="/" to="/todos" />

But It has 2 issues

  1. It redirects me on the todos route on refresh
  2. It gives me following warning

You tried to redirect to the same route you're currently on: "/todos"

I want to achieve the redirection concept as it is in Angular like we do:

{ path: '', redirectTo: '/heroes', pathMatch: 'full' },

Upvotes: 0

Views: 817

Answers (1)

Daniel Andrei
Daniel Andrei

Reputation: 2684

You'd need to wrap them in a <Switch>, for example:

<Switch>
 <Route exact path="/todos" component={Todo} />
 <Route exact path="/users" component={User} />
 <Redirect from="/" to="/todos" />
</Switch>

It will match the routes in order. So for example: if you have /todos, it will match the first one, if you have /users, it will match the second one, for "/" it will redirect to "/todos"

Also Redirect has from and to props. Your example has path instead of from

Upvotes: 2

Related Questions