


I recently had to create a widget in React that fetches data from multiple API endpoints. As the user clicks around, new data is fetched and marshalled into the UI. But it caused some problems.
One problem quickly became evident: if the user clicked around fast enough, as previous network requests got resolved, the UI was updated with incorrect, outdated data for a brief period of time.
We can debounce our UI interactions, but that fundamentally does not solve our problem. Outdated network fetches will resolve and update our UI with wrong data up until the final network request finishes and updates our UI with the final correct state. The problem becomes more evident on slower connections. Furthermore, we’re left with useless networks requests that waste the user’s data.
Here is an example I built to illustrate the problem. It grabs game deals from Steam via the cool Cheap Shark API using the modern fetch() method. Try rapidly updating the price limit and you will see how the UI flashes with wrong data until it finally settles.
The solution
Turns out there is a way to abort pending DOM asynchronous requests using an AbortController. You can use it to cancel not only HTTP requests, but event listeners as well.
The AbortController interface represents a controller object that allows you to abort one or more Web requests as and when desired.
—Mozilla Developer Network
The AbortController API is simple: it exposes an AbortSignal that we insert into our fetch() calls, like so:
const abortController = new AbortController() const signal = abortController.signal fetch(url, { signal })
From here on, we can call abortController.abort() to make sure our pending fetch is aborted.
Let’s rewrite our example to make sure we are canceling any pending fetches and marshalling only the latest data received from the API into our app:
The code is mostly the same with few key distinctions:
- It creates a new cached variable, abortController, in a useRef in the
component. - For each new fetch, it initializes that fetch with a new AbortController and obtains its corresponding AbortSignal.
- It passes the obtained AbortSignal to the fetch() call.
- It aborts itself on the next fetch.
const App = () => { // Same as before, local variable and state declaration // ... // Create a new cached variable abortController in a useRef() hook const abortController = React.useRef() React.useEffect(() => { // If there is a pending fetch request with associated AbortController, abort if (abortController.current) { abortController.abort() } // Assign a new AbortController for the latest fetch to our useRef variable abortController.current = new AbortController() const { signal } = abortController.current // Same as before fetch(url, { signal }).then(res => { // Rest of our fetching logic, same as before }) }, [ abortController, sortByString, upperPrice, lowerPrice, ]) }
Conclusion
That’s it! We now have the best of both worlds: we debounce our UI interactions and we manually cancel outdated pending network fetches. This way, we are sure that our UI is updated once and only with the latest data from our API.
Das obige ist der detaillierte Inhalt vonSo stornieren Sie anstehende API -Anfragen, um korrekte Daten anzuzeigen. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Hier ist ein Container mit einigen Kinderelementen:

Flyout -Menüs! Das zweite, das Sie für ein Menü implementieren müssen, das ein Hover -Ereignis verwendet, um mehr Menüelemente anzuzeigen. Zum einen sollten sie

"Die Kraft des Web liegt in seiner Universalität. Zugang von allen unabhängig von Behinderung ist ein wesentlicher Aspekt."- Tim Berners-Lee

In der Roundup:#039: DatePickers geben Tastaturbenutzer Kopfschmerzen, einen neuen Webkomponenten -Compiler, der bei der Bekämpfung von Fouc hilft.

Die kurze Antwort: Flex-Shrink und Flex-Basis sind wahrscheinlich das, worauf Sie suchen.

Sie können nicht positioniert werden: klebrig; A

In der Welt der Web -Plattform -Nachrichten, in der die Google Search Console in der Welt der Web -Plattform -News -Such -Markup umgeht, lernen wir, dass benutzerdefinierte Eigenschaften einfacher werden können

Das IndieWeb ist eine Sache! Sie haben eine Konferenz und alles bekommen. Der New Yorker schreibt sogar darüber:


Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

MantisBT
Mantis ist ein einfach zu implementierendes webbasiertes Tool zur Fehlerverfolgung, das die Fehlerverfolgung von Produkten unterstützen soll. Es erfordert PHP, MySQL und einen Webserver. Schauen Sie sich unsere Demo- und Hosting-Services an.

PHPStorm Mac-Version
Das neueste (2018.2.1) professionelle, integrierte PHP-Entwicklungstool

Dreamweaver CS6
Visuelle Webentwicklungstools

SecLists
SecLists ist der ultimative Begleiter für Sicherheitstester. Dabei handelt es sich um eine Sammlung verschiedener Arten von Listen, die häufig bei Sicherheitsbewertungen verwendet werden, an einem Ort. SecLists trägt dazu bei, Sicherheitstests effizienter und produktiver zu gestalten, indem es bequem alle Listen bereitstellt, die ein Sicherheitstester benötigen könnte. Zu den Listentypen gehören Benutzernamen, Passwörter, URLs, Fuzzing-Payloads, Muster für vertrauliche Daten, Web-Shells und mehr. Der Tester kann dieses Repository einfach auf einen neuen Testcomputer übertragen und hat dann Zugriff auf alle Arten von Listen, die er benötigt.

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen