matchRoutes
declare function matchRoutes(
routes: RouteObject[],
location: Partial<Location> | string,
basename?: string
): RouteMatch[] | null;
interface RouteMatch<ParamKey extends string = string> {
params: Params<ParamKey>;
pathname: string;
route: RouteObject;
}
matchRoutes
對應一組路由執行路由比對演算法,針對所提供的 location
,找出有哪些路由(如果有)相符。如果找到相符項目,則會傳回一個 RouteMatch
物件陣列,其中每一個物件都對應一個相符的路由。
這是 React Router 比對演算法的核心功能。此功能由 useRoutes
和 <Routes>
元件在內部使用,用於判斷哪個路由與目前的位置相符。在某些情況下,手動比對一組路由時,此功能也會非常有用。