Rely on useState to bail out when params are unchanged #3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Up to now, passing default request params to
useResource
as a dependency would make it store the last used values, so we could use it to make a deep comparison on every following update attempt to avoid unnecessary calls to the API. On low connections and in fast-changing params scenario though, updating the params before completing a request could make it to be cancelled, even in cases of a false-positive param update (user triggered an update without changing any value, making the deep comparison to fail, but the effect to run the clean up phase anyway).To solve this, we make use of useState capability to bail out of rendering if the value to be updated is the same as the previous one. And thus use the current value as a dependency for the effect that does the actual call.
Although we've been supporting all versions of React that have included hooks, the bail out on useState is available only on 16.8+. Therefore, it's safer to call this a major and prevent impacting those who are using this utility, in a dangerous way.