'matchPath'

This function tells whether a pattern matches a given pathname. It's the same matching logic <Route> and useMatch() use internally, exposed as a plain utility for when you need to test a path outside of rendering.

The argument order is pattern first, then pathname – the opposite order from React Router 5. The pattern can be a plain string, or an object for more control ({ path, caseSensitive, end }); "exact"/"strict" no longer exist since matching is exact by default (end: true, effectively) unless you set end: false.


import { matchPath } from "react-router";

const match = matchPath(
  { path: "/users/:id", caseSensitive: false, end: true },
  "/users/123"
);

// sample returned 'match' object:
// {
//   params: { id: "123" },
//   pathname: "/users/123",
//   pathnameBase: "/users/123",
//   pattern: { path: "/users/:id", caseSensitive: false, end: true }
// }

'null' is returned when the provided pattern does not match the pathname.