Skip to content

fix: remove the keydown capture listener on unmount - #1259

Open
rodrigofariow wants to merge 1 commit into
GoogleMapReact:masterfrom
rodrigofariow:fix/remove-keydown-capture-listener
Open

rodrigofariow wants to merge 1 commit into
GoogleMapReact:masterfrom
rodrigofariow:fix/remove-keydown-capture-listener

Conversation

@rodrigofariow

Copy link
Copy Markdown

componentDidMount adds the window keydown listener with capture: true, but componentWillUnmount removes it without the capture flag. removeEventListener only removes a listener whose capture flag matches, so the listener is never removed.

Why it matters: the leftover listener closes over the GoogleMap instance, and through its props over everything it rendered (markers, clusters), so every unmounted map stays in memory. Counting capture keydown listeners on window across 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 listener componentDidMount added. Nothing changes while the map is mounted.

Verification:

  • New test in GoogleMap.spec.js mounts and unmounts the map and checks that keydown is removed with the same listener and capture flag it was added with. It fails on master and passes with the fix (yarn test:unit -t keydown).
  • The six existing tests in that file already fail on master (React 19 removed TestUtils.renderIntoDocument), so the new test uses createRoot + act. yarn test:lint shows one prettier error at google_map.js:708 that is also on master. yarn build passes.
  • To check by hand: mount and unmount the map N times in Chrome, then run getEventListeners(window).keydown.length in the DevTools console. Before the fix it goes up by one per cycle; after the fix it stays flat.

🤖 Generated with Claude Code

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant