@@ -19,13 +19,8 @@ function useResolvedElement<T extends HTMLElement>(
19
19
subscriber : ( element : T ) => SubscriberResponse ,
20
20
refOrElement ?: T | RefObject < T > | null
21
21
) : RefCallback < T > {
22
- // The default ref has to be non-conditionally declared here whether or not
23
- // it'll be used as that's how hooks work.
24
- // @see https://reactjs.org/docs/hooks-rules.html#explanation
25
- let ref : RefObject < T > | null = null ; // Default ref
26
- const refElement = useRef < T | null > ( null ) ;
27
22
const callbackRefElement = useRef < T | null > ( null ) ;
28
- const refCallback = useCallback ( ( element : T ) => {
23
+ const refCallback = useCallback < RefCallback < T > > ( ( element ) => {
29
24
callbackRefElement . current = element ;
30
25
callSubscriber ( ) ;
31
26
} , [ ] ) ;
@@ -36,10 +31,12 @@ function useResolvedElement<T extends HTMLElement>(
36
31
let element = null ;
37
32
if ( callbackRefElement . current ) {
38
33
element = callbackRefElement . current ;
39
- } else if ( refElement . current ) {
40
- element = refElement . current ;
41
- } else if ( refOrElement instanceof HTMLElement ) {
42
- element = refOrElement ;
34
+ } else if ( refOrElement ) {
35
+ if ( refOrElement instanceof HTMLElement ) {
36
+ element = refOrElement ;
37
+ } else {
38
+ element = refOrElement . current ;
39
+ }
43
40
}
44
41
45
42
if ( lastReportedElementRef . current === element ) {
@@ -59,11 +56,6 @@ function useResolvedElement<T extends HTMLElement>(
59
56
}
60
57
} ;
61
58
62
- if ( refOrElement && ! ( refOrElement instanceof HTMLElement ) ) {
63
- // Overriding the default ref with the given one
64
- ref = refOrElement ;
65
- }
66
-
67
59
// On each render, we check whether a ref changed, or if we got a new raw
68
60
// element.
69
61
useEffect ( ( ) => {
@@ -72,11 +64,8 @@ function useResolvedElement<T extends HTMLElement>(
72
64
// the current ref value, but there's no guarantee that the ref value will
73
65
// not change later without a render.
74
66
// This may or may not be a problem depending on the specific use case.
75
- if ( ref ) {
76
- refElement . current = ref . current ;
77
- }
78
67
callSubscriber ( ) ;
79
- } , [ ref , ref ?. current , refOrElement ] ) ;
68
+ } , [ refOrElement ] ) ;
80
69
81
70
return refCallback ;
82
71
}
0 commit comments