React · Middle · Question 13 of 75

How do you avoid a race when loading data in useEffect if userId changes quickly?

Explanation

Responses can arrive out of order, and an old response overwrites a new one. In cleanup, set ignore = true (the result is ignored) or call controller.abort() (the request is cancelled). Even better, use TanStack Query or framework data loading, where this is solved.

Further reading

Problem with a question

React · Middle · question 13

Test yourself

Take the React · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz