React-router v6. How is matchPath supposed to work now

Viewed 5431
1 Answers

It looks like in react-router-dom@6 the order of the arguments passed was inverted.

matchPath

declare function matchPath<
  ParamKey extends string = string
>(
  pattern: PathPattern | string,
  pathname: string
): PathMatch<ParamKey> | null;

interface PathMatch<ParamKey extends string = string> {
  params: Params<ParamKey>;
  pathname: string;
  pattern: PathPattern;
}

interface PathPattern {
  path: string;
  caseSensitive?: boolean;
  end?: boolean;
}

pattern is the first argument, pathname is the second.

You've passed pathname then pattern:

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

To resolve swap the order of arguments passed to matchPath:

const match = matchPath(
  { path: "/users/:id" },
  "/users/123",
);
Related