Problem: If the user navigates away before a fetch completes, it creates a...


Problem: If the user navigates away before a fetch completes, it creates a race condition. They’ll see the “abandoned” request’s data if it returns last. 😬

Solution: Cancel abandoned requests via AbortController. 😎

It’s a 3 step process:

Alternatively, you can handle this via a "didCancel" variable in useEffect (set didCancel to true in useEffect's cleanup function).

Better yet, tools like react-query and @remix_run handle this automatically. 👍

More details here: https://wanago.io/2022/04/11/abort-controller-race-conditions-react/

View original on X