Async/sync location updates after history back/forward
See original GitHub issueVersion
4.2.2
Steps to reproduce
If I understand correctly:
history.goBackdelegates straight to the History API.- Immediately afterwards,
window.locationrefers to the previous page. - When the
popstateevent is emitted (viahistory.listen),Routecomponents will be updated with the newlocationprop (derived from the currentwindow.location).
I have a modal component which calls history.goBack when it’s closed, just like the example here: https://reacttraining.com/react-router/web/example/modal-gallery.
My problem:
- Components may re-render themselves any number of times between
history.goBack(1, above) being called and thepopstateevent being emitted (3). - During these re-renders, the
locationprop will refer to the oldlocation(the one prior to thehistory.goBackcall).
This means the modal will remain mounted (because the current location is still the modal), and the user may try to close the modal again, resulting in another call to history.goBack.
- user comes to our site (
/) from another site (e.g. Google) - user clicks through to a modal
location(/modal) - user closes modal. URL immediately updates (
/), but old modallocationremains (/modal; due to nopopstateevent yet), thus modal remains mounted - user tries to close modal again, and location reverts to Google (!)
I’m wondering if React Router should synchronously/immediately update route components with the new location after a call to history.go{Back,Forward}, rather than waiting for the popstate event (which is asynchronous). This should eliminate problems like the example I gave above.
(I’m also wondering why there is such a long delay between history.goBack and popstate—perhaps because we have a lot of other work to complete in the stack frame that history.goBack is called.)
(I’m hoping I haven’t misdiagnosed the problem here…)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:9 (3 by maintainers)

Top Related StackOverflow Question
@pshrmn Thanks, that is what I ended up doing, but since the official modal example follows the same approach, I thought about raising it as an issue.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.