Always cancel your XHR requests before firing a new one. It solves lots of bugs.
The native XHR object has an abort
method. Use a library that either gives you access to the xhr
object, or wraps it
with some other system for aborting.
Then,
- Attempt to cancel the last request before firing a new one whenever the user can initiate an action (such as clicking a button to fetch data)
- Cancel all requests for a component in
componentWillUnmount
When two requests with the same "purpose" are sent at once, they may not return in the same order they were sent. You can write code to keep track of the requests, but in my experience it takes less code to cancel the old requests instead.
See below
Here is an idea for an "abortable fetch":