Fix race conditions and add tests #85
Merged
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.
(This PR will also move the default branch from
master
tomain
.)Changelog
Added
Fixed
useScrollTo()
to any positions, the scroll view may scroll back to the bottom immediately, in PR #85Description
We are using a state value (i.e.
useState()
) in asetInterval()
.If the system is under heavy load, our stickiness check that runs every 17 ms may get a outdated
sticky
value.We should not use any state value in
setInterval
,setTimeout
, anduseCallback
. Instead, we should use ref value.This work will introduce a new hook called
useStateRef
, which return[value, setValue, valueRef]
and works similar touseState()
plus a handy ref value.Root cause
This is a sample repro.
Under heavy load, after clicking the button. The console log may show:
Although
value
changed, under heavy load, React may postpone callingApp()
to next frame since React Fiber can only run for 17 ms.Thus, the
useEffect
is not called and interval was not reset thruclearInterval
followed bysetInterval
.Thus, when the interval function runs, it is still using the old
value
, which is 1 (on the denoted line).To fix it, we should use ref value in the interval function. This also helps with performance because the interval function was not re-registered.