Last updated: Apr 7, 2024
Reading timeยท3 min
Use a wildcard placeholder to handle 404 page not found in React router, e.g.
<Route path="*" element={<PageNotFound />} />
.
A route that has an asterisk path *
serves as a catch-all route. It only
matches when no other routes do.
import React from 'react'; import {Route, Link, Routes} from 'react-router-dom'; export default function App() { return ( <div> <div> <nav> <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/about">About</Link> </li> <li> {/* ๐๏ธ Link to catch all route */} <Link to="/does-not-exist">Catch all route</Link> </li> </ul> </nav> <Routes> <Route path="/about" element={<About />} /> <Route path="/" element={<Home />} /> {/* ๐๏ธ Only match this when no other routes match */} <Route path="*" element={<PageNotFound />} /> </Routes> </div> </div> ); } function Home() { return <h2>Home</h2>; } function About() { return <h2>About</h2>; } function PageNotFound() { return ( <div> <h2>404 Page not found</h2> </div> ); }
We used a no match route.
{/* ๐๏ธ Only match this when no other routes match */} <Route path="*" element={<PageNotFound />} />
The *
wildcard syntax can only be used at the end of a path.
*
serves as a catch-all route. It only matches when no other routes do.If the user navigates to /
or /about
, they will get the component that we
set for these routes, however, if they navigate to any other page, they would
get the PageNotFound
component.
When using React router hooks, make sure to wrap your application in a Router
component in your index.js
file.
import {createRoot} from 'react-dom/client'; import App from './App'; import {BrowserRouter as Router} from 'react-router-dom'; const rootElement = document.getElementById('root'); const root = createRoot(rootElement); // ๐๏ธ Wrap the App in a Router root.render( <Router> <App /> </Router> );
Router
component is in your index.js
file because that's the entry point of your React application.Once your entire app is wrapped with a Router
component, you can use any of
the hooks from the React router package anywhere in your components.
An alternative way to handle "404 page not found" in React Router is to redirect the user to a different page when they try to go to a page that doesn't exist.
import React from 'react'; import {Route, Link, Routes, Navigate} from 'react-router-dom'; export default function App() { return ( <div> <div> <nav> <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/about">About</Link> </li> <li> {/* ๐๏ธ Link to catch all route */} <Link to="/does-not-exist">Catch all route</Link> </li> </ul> </nav> <Routes> <Route path="/about" element={<About />} /> <Route path="/" element={<Home />} /> {/* ๐๏ธ Only match this when no other routes match */} <Route path="*" element={<Navigate to="/" />} /> </Routes> </div> </div> ); } function Home() { return <h2>Home</h2>; } function About() { return <h2>About</h2>; }
In the example, instead of rendering a PageNotFound
component, we redirect the
user to /
if they go to a route that doesn't exist.
<Route path="*" element={<Navigate to="/" />} />
We used the Navigate
element to redirect the user to /
every time they go to a route that doesn't
have any matches.