@@ -27,9 +27,11 @@ const useCookieListener = (
27
27
options : ICookieOptions = defaultOptions
28
28
) => {
29
29
const { timeout, immediately } = options ;
30
+ const isWatchAll = ! watchFields . length ;
30
31
const timerRef = useRef < number > ( ) ;
31
32
const currentCookiesRef = useRef < string > ( document . cookie ) ;
32
- const isWatchAll = ! watchFields . length ;
33
+ const handlerRef = useRef < CompareCookieHandler > ( ) ;
34
+ handlerRef . current = handler ;
33
35
34
36
useEffect ( ( ) => {
35
37
timerRef . current = window . setInterval ( ( ) => {
@@ -53,15 +55,15 @@ const useCookieListener = (
53
55
changedFields . push ( { key, value : newValue } ) ;
54
56
}
55
57
}
56
- changedFields . length && handler ( { changedFields, prevCookies, nextCookies } ) ;
58
+ changedFields . length && handlerRef . current ?. ( { changedFields, prevCookies, nextCookies } ) ;
57
59
} ;
58
60
59
61
const compareValue = ( ) => {
60
62
const prevCookies = currentCookiesRef . current ;
61
63
const nextCookies = document . cookie ;
62
64
if ( prevCookies !== nextCookies ) {
63
65
isWatchAll
64
- ? handler ( { prevCookies, nextCookies } )
66
+ ? handlerRef . current ?. ( { prevCookies, nextCookies } )
65
67
: handleFieldsChange ( prevCookies , nextCookies ) ;
66
68
currentCookiesRef . current = nextCookies ;
67
69
}
0 commit comments