Sub topic 6
Index routes and the catch-all 404 route
This chapter assumes you can define route objects, nest child routes under a layout, and render children with Outlet. Two special routes round out a route tree: the index route, which fills a layout when no child path matches, and the splat route, which catches every other URL.
The index route
A layout route at / renders its Outlet. When the URL is exactly /, no child has a path to match, so the Outlet stays empty. An index route fixes this. It has index: true and no path. It renders in the parent's Outlet when the URL matches the parent path and nothing deeper.
import { createBrowserRouter, RouterProvider, Link, Outlet } from "react-router-dom";
function Layout() {
return (
<div>
<nav>
<Link to="/">Home</Link> <Link to="/about">About</Link>
</nav>
<Outlet />
</div>
);
}
const router = createBrowserRouter([
{
path: "/",
element: <Layout />,
children: [
{ index: true, element: <h1>Home</h1> },
{ path: "about", element: <h1>About</h1> },
],
},
]);
export default function App() {
return <RouterProvider router={router} />;
}Index routes work at any depth. A users route with children can have its own index route to show a "Pick a user" message at /users.
const usersRoute = {
path: "users",
element: <Outlet />,
children: [
{ index: true, element: <p>Pick a user.</p> },
{ path: ":userId", element: <p>User details</p> },
],
};An index route cannot have children, and it cannot have a path. Its URL is the parent's URL.
The catch-all route
A path of * is a splat. It matches any URL, including multiple segments such as /a/b/c. React Router ranks routes by specificity, so * loses to any more specific match. You can place it anywhere in the array, and it will only render when nothing else fits.
function NotFound() {
return (
<div>
<h1>404</h1>
<p>This page does not exist.</p>
<Link to="/">Go home</Link>
</div>
);
}
const router = createBrowserRouter([
{
path: "/",
element: <Layout />,
children: [
{ index: true, element: <h1>Home</h1> },
{ path: "about", element: <h1>About</h1> },
{ path: "*", element: <NotFound /> },
],
},
]);Here the splat is a child of the layout, so the nav bar still shows on the 404 page. Put it at the top level instead if you want a bare 404 page with no layout.
Common errors
- Blank content at the parent URL. You forgot the index route, so the
Outletrenders nothing. - An index route with a
path. Use eitherindex: trueorpath, not both. - 404 status code. The splat renders a 404 page, but the browser still receives HTTP 200 from your static host. Client-side routing cannot change that status.
- Using
*for unknown IDs./users/999matchesusers/:userId, so the splat never runs. Handle missing data in the loader, as the error-handling chapter shows.
Exercise
A layout route at "/" has children { path: "about" } and { path: "*" }. A visitor opens "/". What does the layout's Outlet show, and what single change makes it show a home page?
Show answer
The splat route matches "/", so the Outlet shows the 404 page, because no child matches the exact parent URL. Adding an index route, { index: true, element: <Home /> }, to the children makes "/" render the home page, since an index route is more specific than the splat.