Ad

Why Does The React Router History Length Increase On Refresh?

- 1 answer

The first time I open the app, the length of the history object (returned by the useHistory Hook) is 3.

On every further refresh, the length gets incremented. Why is that? Does this happen in production too?

Environment:

Ad

Answer

If you navigate to the site in a tab where you have been visiting other pages, history.length will be >1. history.length is the number of locations in the joint session history, not the current site's session history.

Even opening a new tab and entering the url of your app will mean a history.length of 2: blank tab + your url. You can get a length of 1 if you arrive directly at the page through a link on a new window/tab.

This may explain >1 length when first opening your app. This doesn't answer why your app increases the length on refresh, so it may have to do with your specific structure.

Ad
source: stackoverflow.com
Ad