question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async/sync location updates after history back/forward

See original GitHub issue

Version

4.2.2

Steps to reproduce

If I understand correctly:

  1. history.goBack delegates straight to the History API.
  2. Immediately afterwards, window.location refers to the previous page.
  3. When the popstate event is emitted (via history.listen), Route components will be updated with the new location prop (derived from the current window.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 the popstate event being emitted (3).
  • During these re-renders, the location prop will refer to the old location (the one prior to the history.goBack call).

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.

  1. user comes to our site (/) from another site (e.g. Google)
  2. user clicks through to a modal location (/modal)
  3. user closes modal. URL immediately updates (/), but old modal location remains (/modal; due to no popstate event yet), thus modal remains mounted
  4. 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
OliverJAshcommented, Dec 13, 2018

@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.

0reactions
stale[bot]commented, Sep 10, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async/sync location updates after history back/forward #6512
I'm wondering if React Router should synchronously/immediately update route components with the new location after a call to history.go{Back, ...
Read more >
Async Navigation in Single-Page Applications - Blog
With an asynchronous single-page application, changing locations is a side effect of navigation; the location isn't updated until any async ...
Read more >
History.go() - Web APIs - MDN Web Docs - Mozilla
The History.go() method loads a specific page from the session history. You can use it to move forwards and backwards through the history...
Read more >
CLLocationManager is asynchronous. Having problems with this
An array of CLLocation objects containing the location data. This array always contains at least one object representing the current location.
Read more >
BizTalk Patterns part 2 – Sync Async - Microsoft Community Hub
Developing with BizTalk Server, like. ... and the server sends back a PIP3A6 order status update (Distribute Order Status) some time later.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found