Compose nested routes and not-found UI
Compose a layout with child routes
Nested routes let related pages share a layout. A parent path supplies the path prefix and the parent element decides where its matched child is displayed using Outlet. This is different from passing ordinary children props: the router selects the child from the current location. An index route is a default child at the parent URL and cannot itself have child routes.
import React from 'react';import { MemoryRouter, Routes, Route, Link, Outlet } from 'react-router';function TrailLayout() { return <main><h1>Trail center</h1><nav aria-label="Trail section"> <Link to=".">Overview</Link>{' | '}<Link to="equipment">Equipment</Link> </nav><Outlet /></main>;}export default function App() { return <MemoryRouter initialEntries={['/trails/equipment']}><Routes> <Route path="trails" element={<TrailLayout />}> <Route index element={<h2>Choose a route</h2>} /> <Route path="equipment" element={<h2>Bring a map</h2>} /> </Route> </Routes></MemoryRouter>;}The initial screen displays Trail center and Bring a map. Overview should navigate to /trails and display Choose a route inside the same layout. Relative links resolve using the route context by default; a leading slash instead targets an absolute app path. If Outlet is missing, the parent can match and render while its child has nowhere to appear. A pathless layout can group pages without adding a URL segment.
Handle unmatched locations
import React from 'react';import { MemoryRouter, Routes, Route, Link } from 'react-router';function NotFound() { return <main><h1>Page not found</h1><p>Choose a known destination.</p><Link to="/">Return home</Link></main>;}export default function App() { return <MemoryRouter initialEntries={['/unknown/path']}><Routes> <Route path="/" element={<main><h1>Trail guide</h1></main>} /> <Route path="*" element={<NotFound />} /> </Routes></MemoryRouter>;}The wildcard matches a location not handled by a more specific route. It shows a recovery link instead of a blank page. A wildcard UI is not an HTTP 404 response: a static SPA host may already have returned its HTML shell with status 200. With BrowserRouter, a reload of /trails/equipment requests that path from the hosting server. Configure a fallback for actual app routes, while leaving asset and API failures as genuine errors. Verify direct URLs, refresh and missing assets in the chosen hosting environment. Client routing never enforces backend authorization.
Update the page title and focus
import React, { useEffect, useRef } from 'react';import { MemoryRouter, Routes, Route, NavLink } from 'react-router';function Page({title, description}) { const heading = useRef(null); useEffect(() => { document.title = `${title} | Trail guide`; heading.current?.focus(); }, [title]); return <main><h1 ref={heading} tabIndex={-1}>{title}</h1><p>{description}</p></main>;}export default function App() { return <MemoryRouter><nav aria-label="Main"><NavLink to="/" end>Home</NavLink>{' | '}<NavLink to="/rules">Rules</NavLink></nav> <Routes><Route path="/" element={<Page title="Home" description="Choose a trail." />} /> <Route path="/rules" element={<Page title="Rules" description="Stay on marked paths." />} /> <Route path="*" element={<Page title="Not found" description="Use the navigation to continue." />} /></Routes> </MemoryRouter>;}When Rules is activated, the title and heading should reflect Rules; focus moves to the heading after the commit. tabIndex=-1 permits programmatic focus without adding a normal Tab stop. This small demo also moves focus on first mount; real apps should decide when to preserve focus or scroll, especially for Back, hash links, filters and modal navigation. Do not move focus on every keystroke. A full app may use a route announcement region or framework-specific title management. The DOM effect is browser-only; no server rendering or accessibility conformance is claimed.
Practice
Add a child Safety route and a section-level wildcard. Verify that the main layout stays present for known and unknown section paths, that every screen has a meaningful title, and that keyboard users can reach the recovery link. Compare a MemoryRouter test with a separately approved BrowserRouter hosting test.