tomoakley / freemeals.uk

MIT License
10 stars 10 forks source link

Code cleanup: add missing key and logic to avoid data race in PostcodeSearch #30

Closed Nathan-Fenner closed 3 years ago

Nathan-Fenner commented 3 years ago

In <LocationFilter />, the <LocationItem> component was missing its key. Also, since .sort() mutates its list, I added .slice() beforehand to avoid unintentionally mutating the locations argument.

Separately, in <PostcodeSearch />, extra logic is added to avoid a data race in the async function called in useEffect. Specifically, if the user has a slow connection and changes the postcode quickly, then the requests might resolve out-of-order, causing an earlier request to overwrite a later one. To prevent this, we can track whether the current request is still current by adding a current local variable, and updating it in the returned "cleanup" callback. This transformation could probably be applied more productively elsewhere, but I haven't investigated the rest of the codebase yet.