Repository navigation
fix: remove the keydown capture listener on unmount - #1259
Open
rodrigofariow wants to merge 1 commit into
Open
rodrigofariow wants to merge 1 commit into
rodrigofariow wants to merge 1 commit into
Conversation
componentDidMount adds the window keydown listener with capture: true, but componentWillUnmount removed it without the capture flag, so the removal never matched and the listener (and the unmounted GoogleMap it closes over) stayed alive. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
componentDidMountadds thewindowkeydownlistener withcapture: true, butcomponentWillUnmountremoves it without the capture flag.removeEventListeneronly removes a listener whose capture flag matches, so the listener is never removed.Why it matters: the leftover listener closes over the
GoogleMapinstance, and through its props over everything it rendered (markers, clusters), so every unmounted map stays in memory. Counting capturekeydownlisteners onwindowacross five mount/unmount cycles gives 1, 2, 3, 4, 5 with the current code and 0 after each unmount with this fix.Change: one argument,
window.removeEventListener('keydown', this._onKeyDownCapture, true). With the matching capture flag it removes exactly the listenercomponentDidMountadded. Nothing changes while the map is mounted.Verification:
GoogleMap.spec.jsmounts and unmounts the map and checks thatkeydownis removed with the same listener and capture flag it was added with. It fails onmasterand passes with the fix (yarn test:unit -t keydown).master(React 19 removedTestUtils.renderIntoDocument), so the new test usescreateRoot+act.yarn test:lintshows one prettier error atgoogle_map.js:708that is also onmaster.yarn buildpasses.getEventListeners(window).keydown.lengthin the DevTools console. Before the fix it goes up by one per cycle; after the fix it stays flat.🤖 Generated with Claude Code