@@ -28,9 +28,11 @@ const useCookieListener = (
2828 options : ICookieOptions = defaultOptions
2929) => {
3030 const { timeout, immediately } = options ;
31+ const isWatchAll = ! watchFields . length ;
3132 const timerRef = useRef < number > ( ) ;
3233 const currentCookiesRef = useRef < string > ( document . cookie ) ;
33- const isWatchAll = ! watchFields . length ;
34+ const handlerRef = useRef < CompareCookieHandler > ( ) ;
35+ handlerRef . current = handler ;
3436
3537 useEffect ( ( ) => {
3638 timerRef . current = window . setInterval ( ( ) => {
@@ -54,15 +56,15 @@ const useCookieListener = (
5456 changedFields . push ( { key, value : newValue } ) ;
5557 }
5658 }
57- changedFields . length && handler ( { changedFields, prevCookies, nextCookies } ) ;
59+ changedFields . length && handlerRef . current ?. ( { changedFields, prevCookies, nextCookies } ) ;
5860 } ;
5961
6062 const compareValue = ( ) => {
6163 const prevCookies = currentCookiesRef . current ;
6264 const nextCookies = document . cookie ;
6365 if ( prevCookies !== nextCookies ) {
6466 isWatchAll
65- ? handler ( { prevCookies, nextCookies } )
67+ ? handlerRef . current ?. ( { prevCookies, nextCookies } )
6668 : handleFieldsChange ( prevCookies , nextCookies ) ;
6769 currentCookiesRef . current = nextCookies ;
6870 }
0 commit comments