Skip to content

Commit

Permalink
chore: attach original route object to routesMeta (#8171)
Browse files Browse the repository at this point in the history
  • Loading branch information
baozouai committed Dec 9, 2021
1 parent 75f8e2d commit 9412869
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions packages/react-router/index.tsx
Expand Up @@ -862,7 +862,7 @@ export function matchRoutes(

let matches = null;
for (let i = 0; matches == null && i < branches.length; ++i) {
matches = matchRouteBranch(branches[i], routes, pathname);
matches = matchRouteBranch(branches[i], pathname);
}

return matches;
Expand All @@ -872,6 +872,7 @@ interface RouteMeta {
relativePath: string;
caseSensitive: boolean;
childrenIndex: number;
route: RouteObject;
}

interface RouteBranch {
Expand All @@ -890,7 +891,8 @@ function flattenRoutes(
let meta: RouteMeta = {
relativePath: route.path || "",
caseSensitive: route.caseSensitive === true,
childrenIndex: index
childrenIndex: index,
route
};

if (meta.relativePath.startsWith("/")) {
Expand Down Expand Up @@ -993,11 +995,8 @@ function compareIndexes(a: number[], b: number[]): number {

function matchRouteBranch<ParamKey extends string = string>(
branch: RouteBranch,
// TODO: attach original route object inside routesMeta so we don't need this arg
routesArg: RouteObject[],
pathname: string
): RouteMatch<ParamKey>[] | null {
let routes = routesArg;
let { routesMeta } = branch;

let matchedParams = {};
Expand All @@ -1019,7 +1018,7 @@ function matchRouteBranch<ParamKey extends string = string>(

Object.assign(matchedParams, match.params);

let route = routes[meta.childrenIndex];
let route = meta.route;

matches.push({
params: matchedParams,
Expand All @@ -1031,8 +1030,6 @@ function matchRouteBranch<ParamKey extends string = string>(
if (match.pathnameBase !== "/") {
matchedPathname = joinPaths([matchedPathname, match.pathnameBase]);
}

routes = route.children!;
}

return matches;
Expand Down

0 comments on commit 9412869

Please sign in to comment.