Skip to content

Commit 1e2bebb

Browse files
committed
fix(react-router): cleanup sibling views on push navigation to container routes
1 parent 8052ea1 commit 1e2bebb

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

packages/react-router/src/ReactRouter/StackManager.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,19 +442,38 @@ export class StackManager extends React.PureComponent<StackManagerProps> {
442442
}
443443

444444
/**
445-
* Cleans up orphaned sibling views after replace actions (redirects).
445+
* Cleans up orphaned sibling views after replace actions or push-to-container navigations.
446446
*/
447447
private cleanupOrphanedSiblingViews(
448448
routeInfo: RouteInfo,
449449
enteringViewItem: ViewItem,
450450
leavingViewItem: ViewItem | undefined
451451
): void {
452-
if (routeInfo.routeAction !== 'replace') {
452+
const enteringRoutePath = enteringViewItem.reactElement?.props?.path as string | undefined;
453+
if (!enteringRoutePath) {
453454
return;
454455
}
455456

456-
const enteringRoutePath = enteringViewItem.reactElement?.props?.path as string | undefined;
457-
if (!enteringRoutePath) {
457+
const leavingRoutePath = leavingViewItem?.reactElement?.props?.path as string | undefined;
458+
const isContainerRoute = (path: string | undefined) => path?.endsWith('/*');
459+
460+
const isReplaceAction = routeInfo.routeAction === 'replace';
461+
const isPushToContainer =
462+
routeInfo.routeAction === 'push' && routeInfo.routeDirection === 'none' && isContainerRoute(enteringRoutePath);
463+
464+
if (!isReplaceAction && !isPushToContainer) {
465+
return;
466+
}
467+
468+
// Skip cleanup for tab switches
469+
const isSameView = enteringViewItem === leavingViewItem;
470+
const isSameContainerRoute = isContainerRoute(enteringRoutePath) && leavingRoutePath === enteringRoutePath;
471+
const isNavigatingWithinContainer =
472+
isPushToContainer &&
473+
!leavingViewItem &&
474+
routeInfo.prevRouteLastPathname?.startsWith(enteringRoutePath.replace(/\/\*$/, ''));
475+
476+
if (isSameView || isSameContainerRoute || isNavigatingWithinContainer) {
458477
return;
459478
}
460479

0 commit comments

Comments
 (0)