useMatch

useMatch(pattern) asks a question the other hooks cannot: does the current URL match this pattern, whether or not any <Route> was declared for it? It returns null for no match, or a match object — for /:id/:page? against /netflix, that is params: { id: 'netflix' }, pathname and pathnameBase both /netflix, and the resolved pattern. matchPath(pattern, pathname) is the same matcher as a plain function, usable outside React 7,897 entirely — in a loader, a test, a service worker.

Use it for UI that must react to the URL without being routed by it: the custom link of NavLink and Custom Links, a breadcrumb trail, a header that hides itself on /checkout/*. Patterns are exact by default, so append /* for a prefix test. And remember it matches the URL, not the route tree — useMatch('/tasks') is null on /tasks/7 even though the /tasks route is very much rendered.

The removed useRouteMatch() did double duty in React Router 5 139,386 : testing arbitrary patterns, and handing back the current route's path and url for concatenating nested routes and links. Only the first job survives; nested <Route> elements and relative links replaced the second.