Repository navigation
Unmount the canvas-in-layer root when the map is removed - #172
Merged
RodrigoHamuy merged 1 commit intoNov 1, 2026
Merged
Conversation
🦋 Changeset detectedLatest commit: 88838d9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
RodrigoHamuy
enabled auto-merge (squash)
November 1, 2026 13:40
RodrigoHamuy
disabled auto-merge
November 1, 2026 13:40
RodrigoHamuy
approved these changes
Nov 1, 2026
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.
Fixes #171
Problem
<Canvas overlay={false}>only unmounts its R3F root in the custom layeronRemove.map.remove()does not callonRemove(Mapbox and MapLibre only call it fromremoveLayer). react-map-gl's<Layer>also skipsremoveLayeronce the map is gone. So when the whole<Map>unmounts, the root stays in_rootsand no cleanup runs inside the scene.Fix
The layer now also has an
onAdd. It listens to the mapremoveevent and unmounts the root then.onRemovestops listening and unmounts the root, as before.Why this way:
onAdd/onRemove), not React effects. So it does not depend on the order of effect cleanups between<Map>and<Canvas>, and StrictMode only pairs an extra off/on.root.unmount()uses the canvas thatcreateRootgot at creation. Mapbox setsmap._canvastoundefinedbefore it firesremove, somap.getCanvas()must not be read there.onAddfromStyle.addLayer, and fireremoveat the end ofmap.remove().MapEventTypegets aremoveentry for the types.Testing
A repro app (React 19 StrictMode, MapLibre, R3F 9.6.1, react-map-gl 8.1.1) toggles the whole
<Map>3 times, then only the<Canvas>. It reads_roots.size, countstunnel-ratoverlay nodes and clicks the mesh after each mount.<Canvas>unmounted (prod build)Mesh clicks work after each map mount, in dev and in a production build.
Tested with MapLibre only. Mapbox was checked by reading the
mapbox-gl3.24.0 sources.Not in this PR
In dev StrictMode, if
<Canvas>mounts on a map whose style is already loaded, react-map-gl's<Layer>callsremoveLayerin StrictMode's test cleanup. That callsonRemoveand unmounts the root, and the layer is added back with a dead root (events stop working). This happens before and after this PR. A fix probably needs the root to be created again after an unmount, which R3F makes hard because it removes_rootsentries 500 ms later.🤖 Generated with Claude Code