Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/unmount-root-on-map-remove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-three-map": patch
---

Unmount the `<Canvas overlay={false}>` root when the map is removed, so it no longer leaks after the whole map unmounts.
3 changes: 2 additions & 1 deletion src/core/canvas-in-layer/use-canvas-in-layer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ export function useCanvasInLayer(props: CanvasProps,fromLngLat: FromLngLat, map:
latitude, longitude, altitude, fromLngLat,
});

const { onRemove, useThree, r3m } = useRoot(fromLngLat, map, props);
const { onAdd, onRemove, useThree, r3m } = useRoot(fromLngLat, map, props);

const render = useRender({origin, frameloop, useThree, map, r3m});

return {
id: props.id,
beforeId: props.beforeId,
onAdd,
onRemove,
render,
type: 'custom',
Expand Down
8 changes: 7 additions & 1 deletion src/core/canvas-in-layer/use-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ export function useRoot(

})

// map.remove() does not call the layer onRemove
const onAdd = useFunction(() => {
map.on('remove', onRemove);
})

const onRemove = useFunction(() => {
map.off('remove', onRemove);
root.unmount();
})

Expand Down Expand Up @@ -117,5 +123,5 @@ export function useRoot(
</>);
}, [props.children]) // eslint-disable-line react-hooks/exhaustive-deps

return { onRemove, useThree, r3m };
return { onAdd, onRemove, useThree, r3m };
}
1 change: 1 addition & 0 deletions src/core/generic-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface MapInstance {
/** Generic interface of Mapbox/Maplibre `MapEventType` */
export type MapEventType = {
resize: MapEvent;
remove: MapEvent;
};

/** Generic interface of `MapLibreEvent` or `MapBoxEvent` */
Expand Down