@@ -19,15 +19,16 @@ const usePolling = <PollingInput>(
1919 JSON . stringify ( usePollingOptions . input ) !==
2020 prevPollingInputStringified . current ;
2121
22+ const isMounted = useRef ( false ) ;
23+
2224 useEffect ( ( ) => {
23- let isMounted = false ;
2425 if ( usePollingOptions . enabled === false || ! hasPollingInputChanged ) {
2526 return ( ) => {
2627 // noop
2728 } ;
2829 }
2930
30- isMounted = true ;
31+ isMounted . current = true ;
3132
3233 const cleanup = ( ) => {
3334 if ( pollTokenRef . current ) {
@@ -41,10 +42,8 @@ const usePolling = <PollingInput>(
4142 . startPolling ( usePollingOptions . input )
4243 . then ( ( pollToken ) => {
4344 pollTokenRef . current = pollToken ;
44- // TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31880
45- // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
46- cleanupRef . current = usePollingOptions . callback ?.( pollToken ) || null ;
47- if ( ! isMounted ) {
45+ cleanupRef . current = usePollingOptions . callback ?.( pollToken ) ?? null ;
46+ if ( ! isMounted . current ) {
4847 cleanup ( ) ;
4948 }
5049 } ) ;
@@ -55,11 +54,15 @@ const usePolling = <PollingInput>(
5554
5655 // Return a cleanup function to stop polling when the component unmounts
5756 return ( ) => {
58- isMounted = false ;
57+ isMounted . current = false ;
5958 prevPollingInputStringified . current = null ;
6059 cleanup ( ) ;
6160 } ;
62- } , [ hasPollingInputChanged , usePollingOptions . enabled ] ) ;
61+ } , [
62+ usePollingOptions . input ,
63+ hasPollingInputChanged ,
64+ usePollingOptions . enabled ,
65+ ] ) ;
6366} ;
6467
6568export default usePolling ;
0 commit comments