I have following implementation of React routing. How can I create Error page if path not found from routes list ?,
function App() {
return (
<div className="app-container">
<Switch>
{routes.map(({ key, path, crumbs, renderComponent }) => {
return (
<Route key={key} path={path}>
{renderComponent(crumbs)}
</Route>
)
})}
<Redirect to="/add-user" />
</Switch>
</div>
)
}
CodePudding user response:
assuming its V5, It would be something as below...
Show a 404 Not found
<Route path="*">
<FourZeroFour />
</Route>
Redirect if not found
<Route path="*">
<Redirect to="/" /> // replace your home page path
</Route>
If you care, a good blog post on same ...
CodePudding user response:
I think, we should to check the availability of key param. If key param was not exist, let's redirect user to Custom element 404 Page not found.
function App() {
return (
<div className="app-container">
<Switch>
{routes.map(({ key, path, crumbs, renderComponent }) => {
return (
if(key){
<Route key={key} path={path}>
{renderComponent(crumbs)}
</Route>
}
else{
<CustomElement404PageNotFound />
}
)
})}
<Redirect to="/add-user" />
</Switch>
</div>
)
}
Thank you
CodePudding user response:
You can do it with put Route with path "*" append to last one.