From 8fd53b2cb883f50ede896989d9fca6eb0c574d46 Mon Sep 17 00:00:00 2001 From: ShaneK Date: Wed, 8 Oct 2025 09:42:54 -0700 Subject: [PATCH] fix(react-router): addressing some migration issues with memory and hash router --- .../src/ReactRouter/IonReactHashRouter.tsx | 10 +++++--- .../src/ReactRouter/IonReactMemoryRouter.tsx | 13 ++++++---- .../src/ReactRouter/ReactRouterViewStack.tsx | 24 ++++++++++++++++--- .../src/ReactRouter/StackManager.tsx | 7 +++--- .../react/src/components/IonRouterOutlet.tsx | 13 ++++++---- 5 files changed, 48 insertions(+), 19 deletions(-) diff --git a/packages/react-router/src/ReactRouter/IonReactHashRouter.tsx b/packages/react-router/src/ReactRouter/IonReactHashRouter.tsx index 9c9088eab2..a216ee60fb 100644 --- a/packages/react-router/src/ReactRouter/IonReactHashRouter.tsx +++ b/packages/react-router/src/ReactRouter/IonReactHashRouter.tsx @@ -11,7 +11,7 @@ import { HashRouter, useLocation, useNavigationType } from 'react-router-dom'; import { IonRouter } from './IonRouter'; -export const IonReactHashRouter = ({ children }: PropsWithChildren) => { +const RouterContent = ({ children }: PropsWithChildren<{}>) => { const location = useLocation(); const navigationType = useNavigationType(); @@ -43,9 +43,13 @@ export const IonReactHashRouter = ({ children }: PropsWithChildren{children}; +}; + +export const IonReactHashRouter = ({ children, ...routerProps }: PropsWithChildren) => { return ( - - {children} + + {children} ); }; diff --git a/packages/react-router/src/ReactRouter/IonReactMemoryRouter.tsx b/packages/react-router/src/ReactRouter/IonReactMemoryRouter.tsx index f05ece4f05..8f2ff22989 100644 --- a/packages/react-router/src/ReactRouter/IonReactMemoryRouter.tsx +++ b/packages/react-router/src/ReactRouter/IonReactMemoryRouter.tsx @@ -8,12 +8,11 @@ import type { Action as HistoryAction, Location as HistoryLocation } from 'histo import type { PropsWithChildren } from 'react'; import React, { useEffect, useRef } from 'react'; import type { MemoryRouterProps } from 'react-router'; -import { MemoryRouter } from 'react-router'; -import { useLocation, useNavigationType } from 'react-router-dom'; +import { MemoryRouter, useLocation, useNavigationType } from 'react-router'; import { IonRouter } from './IonRouter'; -export const IonReactMemoryRouter = ({ children }: PropsWithChildren) => { +const RouterContent = ({ children }: PropsWithChildren<{}>) => { const location = useLocation(); const navigationType = useNavigationType(); @@ -45,9 +44,13 @@ export const IonReactMemoryRouter = ({ children }: PropsWithChildren{children}; +}; + +export const IonReactMemoryRouter = ({ children, ...routerProps }: PropsWithChildren) => { return ( - - {children} + + {children} ); }; diff --git a/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx b/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx index 4872222d8e..7f1e0e1389 100644 --- a/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx +++ b/packages/react-router/src/ReactRouter/ReactRouterViewStack.tsx @@ -535,9 +535,27 @@ export class ReactRouterViewStack extends ViewStacks { /** * Matches a view with no path prop (default fallback route) or index route. */ - function matchDefaultRoute(): boolean { - // Don't use view items with no path as default matches for different pathnames - // This prevents index routes from incorrectly matching other routes + function matchDefaultRoute(v: ViewItem): boolean { + const childProps = v.routeData.childProps; + + const isDefaultRoute = childProps.path === undefined || childProps.path === ''; + const isIndexRoute = !!childProps.index; + + if (isDefaultRoute || isIndexRoute) { + match = { + params: {}, + pathname, + pathnameBase: pathname === '' ? '/' : pathname, + pattern: { + path: '', + caseSensitive: childProps.caseSensitive ?? false, + end: true, + }, + }; + viewItem = v; + return true; + } + return false; } } diff --git a/packages/react-router/src/ReactRouter/StackManager.tsx b/packages/react-router/src/ReactRouter/StackManager.tsx index 116daebc44..9bc00656fb 100644 --- a/packages/react-router/src/ReactRouter/StackManager.tsx +++ b/packages/react-router/src/ReactRouter/StackManager.tsx @@ -5,7 +5,7 @@ */ import type { RouteInfo, StackContextState, ViewItem } from '@ionic/react'; -import { RouteManagerContext, StackContext, getConfig } from '@ionic/react'; +import { RouteManagerContext, StackContext, generateId, getConfig } from '@ionic/react'; import React from 'react'; import { Route } from 'react-router-dom'; @@ -47,9 +47,8 @@ export class StackManager extends React.PureComponent; ionPage?: boolean; + id?: string; }; interface InternalProps extends Props { @@ -23,16 +25,19 @@ interface InternalState {} class IonRouterOutletContainer extends React.Component { context!: React.ContextType; + private readonly outletId: string; constructor(props: InternalProps) { super(props); + this.outletId = props.id ?? `routerOutlet-${generateId('routerOutlet')}`; } render() { const StackManager = this.context.getStackManager(); const { children, forwardedRef, ...props } = this.props; - - console.log(`[IonRouterOutlet] Rendering with id: "${props.id}", all props:`, Object.keys(props)); + const outletId = props.id ?? this.outletId; + + console.log(`[IonRouterOutlet] Rendering with id: "${outletId}", all props:`, Object.keys(props)); return this.context.hasIonicRouter() ? ( props.ionPage ? ( @@ -40,8 +45,8 @@ class IonRouterOutletContainer extends React.Component ) : ( - - + + {children}