Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: attach original route object inside routesMeta so matchRouteBranch don't need routesArg #8171

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 5 additions & 8 deletions packages/react-router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,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 @@ -797,6 +797,7 @@ interface RouteMeta {
relativePath: string;
caseSensitive: boolean;
childrenIndex: number;
route: RouteObject;
}

interface RouteBranch {
Expand All @@ -815,7 +816,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 @@ -918,11 +920,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 @@ -944,7 +943,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 @@ -956,8 +955,6 @@ function matchRouteBranch<ParamKey extends string = string>(
if (match.pathnameBase !== "/") {
matchedPathname = joinPaths([matchedPathname, match.pathnameBase]);
}

routes = route.children!;
}

return matches;
Expand Down