File tree Expand file tree Collapse file tree 1 file changed +14
-1
lines changed Expand file tree Collapse file tree 1 file changed +14
-1
lines changed Original file line number Diff line number Diff line change @@ -75,6 +75,11 @@ export default function useDropdownMenu(itemCount: number, options?: DropdownMen
7575 return ;
7676 }
7777
78+ // Initialize object to track if the removal happens before the addition of the event listener
79+ const removalTracker = {
80+ removed : false ,
81+ } ;
82+
7883 // This function is designed to handle every click
7984 const handleEveryClick = ( event : MouseEvent ) : void => {
8085 // Make this happen asynchronously
@@ -97,11 +102,19 @@ export default function useDropdownMenu(itemCount: number, options?: DropdownMen
97102 // Add listener
98103 // -> Force it to be async to fix: https://github.com/facebook/react/issues/20074
99104 setTimeout ( ( ) => {
105+ if ( removalTracker . removed ) {
106+ return ;
107+ }
108+
100109 document . addEventListener ( 'click' , handleEveryClick ) ;
101110 } , 1 ) ;
102111
103112 // Return function to remove listener
104- return ( ) : void => document . removeEventListener ( 'click' , handleEveryClick ) ;
113+ return ( ) : void => {
114+ removalTracker . removed = true ;
115+
116+ document . removeEventListener ( 'click' , handleEveryClick ) ;
117+ } ;
105118 } , [ isOpen ] ) ;
106119
107120 // Disable scroll when the menu is opened, and revert back when the menu is closed
You can’t perform that action at this time.
0 commit comments