Handle NaN comparison in useEffect dependency array #3954
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.
Summary
When comparing changes to
useEffect
dependencies, if any dependency isNaN
it will result in the effect always being run, even if the value wasNaN
on the previous render. This is due toNaN !== NaN
in JavaScript.This change handles the
NaN
case using theis
utility introduced in #3776. Presumably we don't want to useObject.is
due to incompatibility with IE11 (thanks for the insight @andrewiggins!). Note that I copied theis
utility, which is also present incompat/src
, but I'm happy to raise it up tosrc/utils
and use it in both places if that's preferred.Issue
Note: this will cause Preact to hang due to infinite re-renders
Expected behavior
Preact runs the effect once, because
value
never changes fromNaN
.Actual behavior
Preact re-renders forever, because it strictly compares
NaN
toNaN
.