Skip to content
Advertisement

how to properly handle input dependent async data calls in react component?

I am currently building some apps using Typescript and React. To this date i have used some smelly workarounds for the following situation which i would like to get rid of. Maybe you know a better way of doing such.

The Setup: Having a React Component which sould display data which is fetched from server on user input. Example use case: fetch the name of a city from an API based on a postal code entered by user. Example implementation:

JavaScript

The problem: As soon as the user input changes, the updateName() is called, which then updates the state on resolved promise. Consider the following case:

  1. The input is changed and a an update promise (A) is created and now pending.
  2. The input is changed again and a new update promise (B) is created.
  3. Promise B is resolved while promise A is still pending.
  4. The state will now be changed to the result of promise B.
  5. Promise A is now resolved and will override the result of Promise B in the state.
  6. The user is now presented with the wrong name (schould be result of B but is result of A)

Are there ways to surpress such behavior? Are there specific ways/libs for doing this in React, typescript or javascript? Or is such kind of input handling generally to be avoided? Which would be a better way or the best way of handling such scenario in gernerally?

Greets and Thanks

EDIT: for the sake of completeness.

My current way of handling such scenarios is introducing a checksum in the component and only update the state if the checksum is still not altered.

JavaScript

Advertisement

Answer

AbortController (standard web feature, not a lib) is good for this, see *** comments:

JavaScript

You can write a utility that handles multiple outstanding requests for different things by wrapping the fetching method, etc. But that’s the basic mechanism for using AbortController for this.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement