How to Show a 404 Page for Unknown URLs in React Router
To show a "Page not found" message in React Router for any URL that matches no route, create a route with path="*" and put a 404 page component in it. If you place it as a child of the layout route that renders your header and other shared parts, the 404 page appears while the header stays visible. This is the approach to use when people who mistype a URL or follow an outdated link should see a helpful message instead of a blank screen.
In my test environment, I checked what appears with and without a path="*" route, using both the declarative mode written with <Routes> and the data mode written with createBrowserRouter.
The test environment was Ubuntu 24.04 with Node.js 22.23.4. I created a project with the react-ts template of npm create vite@latest (React 19.3.1, Vite 8.3.8, TypeScript 7.0.2), added React Router 8.5.1, and wrapped the components in StrictMode for testing. I opened the pages with Chromium 153 driven by Playwright 1.63.0. I did not test React Router's framework mode, server-side rendering, or the behavior of a server serving a production build.
Pages Used in the Test
In every case, I used the following layout and pages. Layout has a header with a row of links and an <Outlet /> that displays the child route. The "Dog list" link in the header points to a page for which I intentionally did not create a route.
// pages.tsx
import { Link, Outlet, useLocation, useParams } from 'react-router';
export function Layout() {
return (
<>
<header>
<Link to="/">トップ</Link> | <Link to="/cats/3">猫の3番</Link> | <Link to="/dogs">犬の一覧</Link>
</header>
<main>
<Outlet />
</main>
</>
);
}
export function Home() {
return <h1>トップ</h1>;
}
export function Cat() {
const { catId } = useParams();
return <h1>猫の{catId}番</h1>;
}
export function NotFound() {
const { pathname } = useLocation();
return (
<>
<h1>ページが見つかりません</h1>
<p>「{pathname}」というページはありません。</p>
<Link to="/">トップへ戻る</Link>
</>
);
}
NotFound is the 404 page. It uses useLocation() to get the path that was opened, shows it, and provides a link back to the top page.
The test main.tsx switches which approach to try based on the first path segment (such as /decl-star) and uses that as the router's basename. The declarative components such as DeclNone sit inside a <BrowserRouter>, and the data-mode router is passed to <RouterProvider>. In the rest of this article, paths are written without the basename.
Without path="*", Nothing Is Displayed
First, I wrote the declarative mode without a path="*" route, as follows.
import { Route, Routes } from 'react-router';
import { Cat, Home, Layout } from './pages';
// No path="*" route
export default function DeclNone() {
return (
<Routes>
<Route element={<Layout />}>
<Route path="/" element={<Home />} />
<Route path="/cats/:catId" element={<Cat />} />
</Route>
</Routes>
);
}
Opening / and /cats/3 displayed "Top" and "Cat 3" below the header. On the other hand, when I opened /nope or /cats/3/extra, which match no route, nothing was displayed, including the header, and <div id="root"> was empty. There was no error; only the following warning appeared in the console.
No routes matched location "/nope"
This warning is a development-only warning that React Router outputs when no route matches (see the source code). It might seem that Layout would render regardless of the URL, but when no route matched, the parent Layout was not rendered either.
Placing a path="*" Route as a Child of the Layout
I added one path="*" route as the last child of Layout.
import { Route, Routes } from 'react-router';
import { Cat, Home, Layout, NotFound } from './pages';
// Place a path="*" route as a child of the layout
export default function DeclStar() {
return (
<Routes>
<Route element={<Layout />}>
<Route path="/" element={<Home />} />
<Route path="/cats/:catId" element={<Cat />} />
<Route path="*" element={<NotFound />} />
</Route>
</Routes>
);
}
Opening /nope displayed NotFound below the header.

* is called a "splat" in the official documentation. The declarative routing guide explains that when a path ends with /*, everything after the / is captured, including any slashes. With path="*" alone, it catches every path that no other route matched. In my tests, NotFound was also displayed for a path like /cats/3/extra, which partially matches /cats/:catId. /cats/3 still showed "Cat 3".
Writing path="*" last is not required. When I placed path="*" first among the children of Layout, the result was the same: / showed "Top", /cats/3 showed "Cat 3", and /nope showed NotFound. React Router first ranks routes by the shape of their paths and only looks at the order in which they were written when the scores are equal. In the source code that scores and orders the routes, a static segment such as cats gets 10 points, a dynamic segment such as :catId gets 3 points, a path containing * loses 2 points, and the * segment itself gets no points. So a path="*" route is chosen only when no other route matches.
In Data Mode, Without a Catch-All, a Default Error Screen Appears
In data mode, written with createBrowserRouter and <RouterProvider>, the behavior when no route matches differs from declarative mode. I wrote it as follows, without path: '*'.
import { createBrowserRouter } from 'react-router';
import type { RouteObject } from 'react-router';
import { Cat, Home, Layout } from './pages';
const pages: RouteObject[] = [
{ path: '/', element: <Home /> },
{ path: '/cats/:catId', element: <Cat /> },
];
// Neither path: '*' nor errorElement
const router = createBrowserRouter([{ element: <Layout />, children: pages }]);
When I passed this router to <RouterProvider router={router} /> and opened /nope, the page did not go blank. Instead, React Router's built-in error screen appeared.

The console showed the error Error handled by React Router default ErrorBoundary:. In data mode, a URL that matches no route is treated as an error with status 404. In the source code that creates this error, statusText is set to Not Found, and the message is No route matches URL followed by the path of the URL that was opened. This screen is the default development error display that is used when a route has no errorElement.
Catching It with errorElement Hides the Header
This error can also be caught by a component passed as the route's errorElement. I attached the following RootError to the outermost route, the one with Layout.
import { createBrowserRouter, isRouteErrorResponse, useRouteError } from 'react-router';
export function RootError() {
const error = useRouteError();
if (isRouteErrorResponse(error)) {
return <h1>{error.status} {error.statusText}</h1>;
}
return <h1>エラーが起きました</h1>;
}
// Attach an errorElement to the outermost route
const router = createBrowserRouter([
{ element: <Layout />, errorElement: <RootError />, children: pages },
]);
When I opened /nope, the screen showed only "404 Not Found". The error received through useRouteError() was one for which isRouteErrorResponse() returned true, with status of 404 and statusText of Not Found. data was an Error object, and its message was No route matches URL followed by the path of the URL that was opened.
However, the header was not displayed. Only RootError was rendered in place of the Layout route that had the errorElement. The official guide on error handling recommends placing a component that shows errors on the outermost route of every app. If you want to show a message for a nonexistent URL while keeping the header visible, use a path: '*' route as shown next.
Placing a path: '*' Route in Data Mode Too
In data mode, as in declarative mode, add a path: '*' route as a child of Layout.
const router = createBrowserRouter([
{
element: <Layout />,
children: [
{ path: '/', element: <Home /> },
{ path: '/cats/:catId', element: <Cat /> },
{ path: '*', element: <NotFound /> },
],
},
]);
For /nope and /cats/3/extra, NotFound was displayed below the header, just as in declarative mode. Because the * route matched, no 404 error occurred, and the default error screen did not appear.
Navigating from a link inside the app gave the same result. I opened the top page and clicked "Dog list" in the header. The URL path changed to /dogs, and "There is no page at "/dogs"." appeared below the header. When I then clicked "Back to top", the "Top" view returned.
Showing a 404 Page Still Returns Status 200 from the Server
The 404 pages above are rendered by React Router inside the browser. When I opened /nope on the Vite development server, the HTTP status the server returned was 200 in every case. Checking with curl gives the same result (I used the path with the test basename included).
$ curl -s -o /dev/null -w "%{http_code} %{content_type}\n" http://localhost:5173/decl-star/nope
200 text/html
The Vite development server uses the default appType value 'spa', which performs SPA fallback and returns index.html even for paths with no file. The decision of which page to show is then made by React Router in the browser. Even when "Page not found" appears on screen, the HTTP status is not 404.
Apart from 404 pages, there are cases such as /cats/999 where a route matches but the data is not found. A path="*" route cannot catch these. The same error handling guide shows how to use data() inside a loader to throw an error with status 404.
The type check (npx tsc -p tsconfig.app.json --noEmit) did not report an error for the code without path="*". A missing catch-all route cannot be noticed until you open the app in a browser, so open a path that does not exist once and confirm that the 404 page appears.





