1
- import { useRef , useEffect } from "react" ;
1
+ import { useRef , useEffect , useCallback } from "react" ;
2
2
3
3
const isBrowser = typeof window !== "undefined" ;
4
4
5
- export type ScrollValue = { x : any ; y : any } ;
5
+ export type ScrollValue = { x : number ; y : number } ;
6
6
7
7
function getScrollPosition ( element : HTMLElement | undefined | null ) : ScrollValue {
8
8
if ( ! isBrowser ) return { x : 0 , y : 0 } ;
@@ -41,26 +41,28 @@ export const useScrollPosition = (props: UseScrollPositionOptions): ScrollValue
41
41
isEnabled ? getScrollPosition ( elementRef ?. current ) : { x : 0 , y : 0 } ,
42
42
) ;
43
43
44
- let throttleTimeout : ReturnType < typeof setTimeout > | null = null ;
44
+ const throttleTimeout = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
45
45
46
- const handler = ( ) => {
46
+ const handler = useCallback ( ( ) => {
47
47
const currPos = getScrollPosition ( elementRef ?. current ) ;
48
48
49
49
if ( typeof callback === "function" ) {
50
50
callback ( { prevPos : position . current , currPos} ) ;
51
51
}
52
52
53
53
position . current = currPos ;
54
- throttleTimeout = null ;
55
- } ;
54
+ throttleTimeout . current = null ;
55
+ } , [ callback , elementRef ] ) ;
56
56
57
57
useEffect ( ( ) => {
58
58
if ( ! isEnabled ) return ;
59
59
60
60
const handleScroll = ( ) => {
61
61
if ( delay ) {
62
- if ( throttleTimeout === null ) {
63
- throttleTimeout = setTimeout ( handler , delay ) ;
62
+ if ( throttleTimeout . current === null ) {
63
+ throttleTimeout . current = setTimeout ( ( ) => {
64
+ handler ( ) ;
65
+ } , delay ) ;
64
66
}
65
67
} else {
66
68
handler ( ) ;
@@ -71,7 +73,12 @@ export const useScrollPosition = (props: UseScrollPositionOptions): ScrollValue
71
73
72
74
target . addEventListener ( "scroll" , handleScroll ) ;
73
75
74
- return ( ) => target . removeEventListener ( "scroll" , handleScroll ) ;
76
+ return ( ) => {
77
+ target . removeEventListener ( "scroll" , handleScroll ) ;
78
+ if ( throttleTimeout . current ) {
79
+ clearTimeout ( throttleTimeout . current ) ;
80
+ }
81
+ } ;
75
82
} , [ elementRef ?. current , delay , isEnabled ] ) ;
76
83
77
84
return position . current ;
0 commit comments