fix(useElementByPoint): avoid a re-render on every frame in multiple mode - #210
Closed
ostapondo wants to merge 1 commit into
Closed
fix(useElementByPoint): avoid a re-render on every frame in multiple mode#210ostapondo wants to merge 1 commit into
ostapondo wants to merge 1 commit into
Conversation
ostapondo
force-pushed
the
fix/element-by-point-frame-rerender
branch
2 times, most recently
from
July 29, 2026 13:49
5b82365 to
f0f9d7e
Compare
…mode elementsFromPoint() allocates a new array on every call, so the rAF loop handed setElement a fresh reference each frame and re-rendered the consuming component roughly 60 times a second even while the pointer sat still. Compare the hit list by contents and keep the previous array when nothing under the point changed. The single-element branch needs no such check - elementFromPoint returns the same node and React bails out on its own. Adds the first tests for this hook, covering both that an unchanged hit list does not re-render and that a changed one still does.
ostapondo
force-pushed
the
fix/element-by-point-frame-rerender
branch
from
July 29, 2026 13:51
f0f9d7e to
b7278e5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
With
multiple: true,useElementByPointre-renders the consuming component onevery animation frame, even when the pointer hasn't moved and the elements under
it haven't changed.
elementsFromPoint()returns a freshly allocated array on each call, so the rAFloop hands
setElementa new reference every frame. React compares by identity,sees a change, and re-renders — roughly 60 times a second, for as long as the
hook is active. The single-element path doesn't have this problem, since
elementFromPoint()returns the same node and React bails out on its own.The fix compares the hit list by contents and keeps the previous array when
nothing changed. I used the functional form of
setElementso the comparisonsees the committed value without needing an extra ref.
Type of Change
Checklist
No doc change — public API and return values are unchanged.
This hook had no spec file, so I added one. Three tests: the element is reported
for the single case, an unchanged hit list no longer re-renders (3 renders over 3
frames on
main, 0 here), and a hit list that genuinely changes still updates —that last one matters, since a comparison like this is exactly the kind of change
that can silently freeze state.