0
votes

Say we have Home, Page A, Page B and Page C. If I do this:

  1. Open Page B
  2. Go Home
  3. Go to Page B

This pushes at least 3 routes in the history object. If I repeat the steps, there will be 6 items. This is true when pushing directly via history.push and also when using the Link component's with the to prop.

The only way to keep complexity under control is by checking the previous location and then doing either history.goBack or history.push: Check history previous location before goBack().

The catch is that managing the history object can become a very complicated task very quickly. Just by adding a navigation bar that is rendered on every page in the app, you add at least "n-1" places from where you can go back home (assuming home is one of the navigation tabs).

Should we be concerned about this and handle the previous location?


Possibly related question: Why does the React Router history length increase on refresh?

Environment:

1
Seems very much of a muchness to me, I'm not entirely sure of what the question is.James
I updated the post and rephrased the question at the endPaul Razvan Berg
I'm not sure I understand the question. The history object is meant to hold the list of pages the user previously visited. If the user visited the same page more than once why shouldn't that page appear in the list more than once?Elan Hamburger
@ElanHamburger Hmm okay, for some reason I thought that it's supposed to hold the minimum number of pages in there. That is, if I have an app with 4 pages, there should always be maximum 4 items in history, and use history.goBack to navigate backward.Paul Razvan Berg
@PaulRazvanBerg I believe history is supposed to mimic browser history. If you go to page A, click a link to page B, click a link to page A, and click the back button twice, you should end up at page A again.Elan Hamburger

1 Answers

0
votes

Thanks to Elan Hamburger's comments, I now understand that the BrowserHistory is meant to behave like this. Normally, in the browser, if you go to page A, then to page B, then back to page A, clicking back 3 times will return you to the home page.

So you shouldn't really mind the size of the history object, at least certainly not if you don't have billions of users.