Add 404 page to React application
React Router can handle routes for your web app in the front end. Although it does not have a 404 page and if you add a generic route without a path, you'll end up rendering that component all the time, no matter what.
<Route component={NotFound} />
<Route exact path="/" component={Home} />
<Route path="/whatever" component={Whatever} />
<Route component={NotFound} />
</Switch>
<Route component={NotFound} />
There is a simple solution to this problem since React Router 4 came out, called the Switch element. First you'll need to import it from react-router-dom along with BrowserRouter, then surround your routes (or some of them) with it, and it will let only one of the contained routes render.
<Switch> <Route exact path="/" component={Home} />
<Route path="/whatever" component={Whatever} />
<Route component={NotFound} />
</Switch>
Megjegyzések
Megjegyzés küldése