React Router’s No Match documentation covers this. You need to import the <Switch>
component, then you can remove the path
attribute altogether.
A
<Switch>
renders the first child<Route>
that matches. A<Route>
with no path always matches
This is the example that uses:
<Router>
<div>
<Switch>
<Route path="https://stackoverflow.com/" exact component={Home}/>
<Redirect from="/old-match" to="/will-match"/>
<Route path="/will-match" component={WillMatch}/>
<Route component={NoMatch}/>
</Switch>
</div>
</Router>
So in your case, you’d simply drop the path="*"
and introduce the <Switch>
:
<Switch>
<Route exact path="https://stackoverflow.com/" component={Home}/>
<Route path="/user" component={User}/>
<Route component={Notfound} />
</Switch>
Remember to include Switch
to your import
statement at the top.