From e2ed0e9e87e46dc688e57fb635eadc139ee67b0b Mon Sep 17 00:00:00 2001 From: Ely Lucas Date: Thu, 7 Nov 2019 09:00:01 -0700 Subject: [PATCH] fix(react): expand the location stack to better support back button, fixes #19748 (#19856) --- .../react-router/src/ReactRouter/Router.tsx | 33 ++++++++++++++----- .../react-router/src/utils/LocationHistory.ts | 29 +++++++++++++--- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/packages/react-router/src/ReactRouter/Router.tsx b/packages/react-router/src/ReactRouter/Router.tsx index d6b735e435..863619bb2d 100644 --- a/packages/react-router/src/ReactRouter/Router.tsx +++ b/packages/react-router/src/ReactRouter/Router.tsx @@ -22,7 +22,7 @@ class RouteManager extends React.Component v.prevId === view.id); viewsToRemove.forEach(v => { // Don't remove if view is currently active @@ -339,15 +349,20 @@ class RouteManager extends React.Component x.pathname.toLowerCase() === url.toLowerCase()); - return last; + pop() { + this.locationHistory.pop(); + } + + replace(location: HistoryLocation) { + this.locationHistory.pop(); + this.locationHistory.push(location); + } + + findLastLocationByUrl(url: string) { + for (let i = this.locationHistory.length - 1; i >= 0; i--) { + const location = this.locationHistory[i]; + if (location.pathname.toLocaleLowerCase() === url.toLocaleLowerCase()) { + return location; + } + } + return undefined; + } + + previous() { + return this.locationHistory[this.locationHistory.length - 2]; + } + + current() { + return this.locationHistory[this.locationHistory.length - 1]; } }