@@ -27,9 +27,11 @@ const useCookieListener = (
2727 options : ICookieOptions = defaultOptions
2828) => {
2929 const { timeout, immediately } = options ;
30+ const isWatchAll = ! watchFields . length ;
3031 const timerRef = useRef < number > ( ) ;
3132 const currentCookiesRef = useRef < string > ( document . cookie ) ;
32- const isWatchAll = ! watchFields . length ;
33+ const handlerRef = useRef < CompareCookieHandler > ( ) ;
34+ handlerRef . current = handler ;
3335
3436 useEffect ( ( ) => {
3537 timerRef . current = window . setInterval ( ( ) => {
@@ -53,15 +55,15 @@ const useCookieListener = (
5355 changedFields . push ( { key, value : newValue } ) ;
5456 }
5557 }
56- changedFields . length && handler ( { changedFields, prevCookies, nextCookies } ) ;
58+ changedFields . length && handlerRef . current ?. ( { changedFields, prevCookies, nextCookies } ) ;
5759 } ;
5860
5961 const compareValue = ( ) => {
6062 const prevCookies = currentCookiesRef . current ;
6163 const nextCookies = document . cookie ;
6264 if ( prevCookies !== nextCookies ) {
6365 isWatchAll
64- ? handler ( { prevCookies, nextCookies } )
66+ ? handlerRef . current ?. ( { prevCookies, nextCookies } )
6567 : handleFieldsChange ( prevCookies , nextCookies ) ;
6668 currentCookiesRef . current = nextCookies ;
6769 }
0 commit comments