From 77bfa7986c1008cd5470ab088b01b878b693e414 Mon Sep 17 00:00:00 2001 From: Sean Perkins Date: Wed, 18 Jan 2023 22:56:55 -0500 Subject: [PATCH] fix(react-router): parameterized routes create new instances --- .../src/ReactRouter/IonRouter.tsx | 1 + .../src/ReactRouter/ReactRouterViewStack.tsx | 14 +++--- .../src/ReactRouter/StackManager.tsx | 43 ++++++++++++++++--- .../react/src/routing/RouteManagerContext.ts | 4 +- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/packages/react-router/src/ReactRouter/IonRouter.tsx b/packages/react-router/src/ReactRouter/IonRouter.tsx index 0f8f0283a9..acb2cd3955 100644 --- a/packages/react-router/src/ReactRouter/IonRouter.tsx +++ b/packages/react-router/src/ReactRouter/IonRouter.tsx @@ -44,6 +44,7 @@ class IonRouterInner extends React.PureComponent { createViewItem: this.viewStack.createViewItem, findViewItemByRouteInfo: this.viewStack.findViewItemByRouteInfo, findLeavingViewItemByRouteInfo: this.viewStack.findLeavingViewItemByRouteInfo, + findRouteMatchByRouteInfo: this.viewStack.findRouteMatchByRouteInfo, addViewItem: this.viewStack.add, unMountViewItem: this.viewStack.remove, }; diff --git a/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx b/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx index fa9e002102..dc355aae92 100644 --- a/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx +++ b/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx @@ -11,6 +11,7 @@ export class ReactRouterViewStack extends ViewStacks { this.findLeavingViewItemByRouteInfo = this.findLeavingViewItemByRouteInfo.bind(this); this.getChildrenToRender = this.getChildrenToRender.bind(this); this.findViewItemByPathname = this.findViewItemByPathname.bind(this); + this.findRouteMatchByRouteInfo = this.findRouteMatchByRouteInfo.bind(this); } createViewItem(outletId: string, reactElement: React.ReactElement, routeInfo: RouteInfo, page?: HTMLElement) { @@ -96,12 +97,8 @@ export class ReactRouterViewStack extends ViewStacks { return children; } - findViewItemByRouteInfo(routeInfo: RouteInfo, outletId?: string, updateMatch?: boolean) { - const { viewItem, match } = this.findViewItemByPath(routeInfo.pathname, outletId); - const shouldUpdateMatch = updateMatch === undefined || updateMatch === true; - if (shouldUpdateMatch && viewItem && match) { - viewItem.routeData.match = match; - } + findViewItemByRouteInfo(routeInfo: RouteInfo, outletId?: string) { + const { viewItem } = this.findViewItemByPath(routeInfo.pathname, outletId); return viewItem; } @@ -115,6 +112,11 @@ export class ReactRouterViewStack extends ViewStacks { return viewItem; } + findRouteMatchByRouteInfo(routeInfo: RouteInfo, outletId?: string) { + const { match } = this.findViewItemByPath(routeInfo.pathname, outletId); + return match; + } + private findViewItemByPath(pathname: string, outletId?: string, forceExact?: boolean, mustBeIonRoute?: boolean) { let viewItem: ViewItem | undefined; let match: ReturnType | undefined; diff --git a/packages/react-router/src/ReactRouter/StackManager.tsx b/packages/react-router/src/ReactRouter/StackManager.tsx index 1acfd48468..aacaebd0ba 100644 --- a/packages/react-router/src/ReactRouter/StackManager.tsx +++ b/packages/react-router/src/ReactRouter/StackManager.tsx @@ -93,6 +93,22 @@ export class StackManager extends React.PureComponent ViewItem; findViewItemByPathname(pathname: string, outletId?: string): ViewItem | undefined; findLeavingViewItemByRouteInfo: (routeInfo: RouteInfo, outletId?: string) => ViewItem | undefined; - findViewItemByRouteInfo: (routeInfo: RouteInfo, outletId?: string, updateMatch?: boolean) => ViewItem | undefined; + findViewItemByRouteInfo: (routeInfo: RouteInfo, outletId?: string) => ViewItem | undefined; + findRouteMatchByRouteInfo: (routeInfo: RouteInfo, outletId?: string) => any | undefined; getChildrenToRender: ( outletId: string, ionRouterOutlet: React.ReactElement, @@ -36,6 +37,7 @@ export const RouteManagerContext = /*@__PURE__*/ React.createContext undefined, findLeavingViewItemByRouteInfo: () => undefined, findViewItemByRouteInfo: () => undefined, + findRouteMatchByRouteInfo: () => undefined, getChildrenToRender: () => undefined as any, goBack: () => undefined, unMountViewItem: () => undefined,