I have the following Router
definition:
<Router>
<Route exact path='/' component={Home}/>
<Route path='/about' component={About}/>
<Route path='/code' component={Code}/>
</Router>
I want any unmapped route (i.e /foo
) to redirect back to root /
.
I tried <Redirect .../>
without success. Also adding a <Route />
without a path=
resulted in a duplicated component on each page.
Just place a redirect at the bottom like this and wrap your routes with Switch
:
<Router>
<Switch>
<Route exact path='/' component={Home}/>
<Route path='/about' component={About}/>
<Route path='/code' component={Code}/>
// Redirect all 404's to home
<Redirect to='/' />
</Switch>
</Router>