score:10

Accepted answer

you can use

import { redirect } from 'react-router-dom';

and add this route inside your switch:

<route render={() => <redirect to={{pathname: "/"}} />} />

it'll catch anything that has no route.

score:4

if you want to redirect home page when type invalid path string so that follow this code....

<switch>
   <route exact path="/" component={home} />
   <route path="/login" component={login} />
   <route path="/signup" component={signup} />
   <route path="/user" component={user} onenter={this.requireauth}/>
   <route path="*" component={home} />
</switch>

here i used * in path that means if invalid path string type then it auto redirect to home page.


Related Query

More Query from same tag