Sub topic 3
Defining routes and how paths match
This chapter assumes you know JavaScript modules, React function components and JSX, and basic hooks. It also assumes you have a router created with createBrowserRouter and rendered with RouterProvider, as in the previous chapter.
Route objects
A route is a plain object. You pass an array of them to createBrowserRouter. Each object describes one URL pattern and what to render for it.
| Property | Purpose |
|---|---|
| `path` | The URL pattern to match |
| `element` | The React element rendered when the path matches |
| `children` | Nested route objects (covered in a later chapter) |
| `index` | Marks a route that matches its parent's exact path |
import { createBrowserRouter, RouterProvider } from "react-router-dom";
const router = createBrowserRouter([
{ path: "/", element: <h1>Home</h1> },
{ path: "/about", element: <h1>About</h1> },
{ path: "/contact", element: <h1>Contact</h1> },
]);
export default function App() {
return <RouterProvider router={router} />;
}When the URL changes, the router compares the pathname against each route's path and renders the element of the match. Nothing else on the page reloads.
How paths match
The router splits the pathname into segments, which are the parts between slashes. It compares them with the segments in your pattern.
- A static segment such as
aboutmust match exactly. - A dynamic segment starts with a colon, such as
:userId. It matches any single segment. A later chapter covers reading its value. - A trailing
*is a splat. It matches everything that remains in the URL. - Matching ignores case and a trailing slash by default, so
/About/matches/about.
const routes = [
{ path: "/users/:userId", element: <h1>One user</h1> },
{ path: "/files/*", element: <h1>Any file path</h1> },
];Here /users/7 matches the first route, but /users/7/edit does not. The pattern has two segments and the URL has three. The second route matches /files/a/b/c, because the splat absorbs the rest.
Ranking: the best match wins
Several patterns can match one URL. The router does not take the first one in the array. It ranks all candidates and picks the most specific one. Static segments score higher than dynamic segments, and dynamic segments score higher than a splat.
const router = createBrowserRouter([
{ path: "/users/:userId", element: <h1>A user</h1> },
{ path: "/users/new", element: <h1>New user</h1> },
]);For /users/new, both routes match. The static route wins, even though it is listed second. So you can reorder routes freely without changing which one renders.
Common errors
- Blank or 404 screen. If no route matches, the router shows a default 404 error. Add a catch-all route to control this page; a later chapter shows how.
- Expecting array order to decide. Ranking decides, not position.
- Extra segments. A path like
/users/:userIdnever matches/users/7/edit. Add a route or a child route for it.
Exercise
Given the routes /blog/:slug and /blog/archive, which route renders for /blog/archive, and why?
Show answer
/blog/archive. Both patterns match, but the router ranks static segments above dynamic ones, so the static route wins regardless of its position in the array.