In a React app, I need to press the back button 2 times in order to go to the previous page

In case you have such an issue, and verify that you don’t call history.push() 2 times, check your content for an iframe.

TL; DR: React tries to minimize re-rendering and will reuse the iframe element by just changing its src. This will update the history object, which is apparently shared with the main page (and would need to press the back button multiple times).

The solution is to add a unique key to the iframe (I used the src URL itself)

Here is a blog post that explains the issue in-depth:

https://www.aleksandrhovhannisyan.com/blog/react-iframes-back-navigation-bug/

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.