Skip to content

Commit 0d0ecc5

Browse files
fix: read elements from refs directly instead of keeping stale values in closure (#1662)
1 parent 1e92535 commit 0d0ecc5

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/hooks/useCombobox/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ function useCombobox(userProps = {}) {
111111
),
112112
useMemo(
113113
() => [menuRef, toggleButtonRef, inputRef],
114-
[menuRef.current, toggleButtonRef.current, inputRef.current],
114+
// dependencies can be left empty because refs are getting mutated
115+
[],
115116
),
116117
)
117118
const setGetterPropCallInfo = useGetterPropsCalledChecker(

src/hooks/useSelect/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ function useSelect(userProps = {}) {
133133
),
134134
useMemo(
135135
() => [menuRef, toggleButtonRef],
136-
[menuRef.current, toggleButtonRef.current],
136+
// dependencies can be left empty because refs are getting mutated
137+
[],
137138
),
138139
)
139140
const setGetterPropCallInfo = useGetterPropsCalledChecker(

src/hooks/utils.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,16 @@ function useMouseAndTouchTracker(
375375
isTouchEnd: false,
376376
})
377377

378+
// the elements should be retrieved the moment they are required because these are refs - they can be mutated
379+
function getDownshiftElements() {
380+
return downshiftElementsRefs.map(ref => ref.current)
381+
}
382+
378383
useEffect(() => {
379384
if (isReactNative || !environment) {
380385
return noop
381386
}
382387

383-
const downshiftElements = downshiftElementsRefs.map(ref => ref.current)
384-
385388
function onMouseDown() {
386389
mouseAndTouchTrackersRef.current.isTouchEnd = false // reset this one.
387390
mouseAndTouchTrackersRef.current.isMouseDown = true
@@ -390,7 +393,7 @@ function useMouseAndTouchTracker(
390393
mouseAndTouchTrackersRef.current.isMouseDown = false
391394

392395
if (
393-
!targetWithinDownshift(event.target, downshiftElements, environment)
396+
!targetWithinDownshift(event.target, getDownshiftElements(), environment)
394397
) {
395398
handleBlur()
396399
}
@@ -409,7 +412,7 @@ function useMouseAndTouchTracker(
409412
!mouseAndTouchTrackersRef.current.isTouchMove &&
410413
!targetWithinDownshift(
411414
event.target,
412-
downshiftElements,
415+
getDownshiftElements(),
413416
environment,
414417
false,
415418
)

0 commit comments

Comments
 (0)