Skip to content

Commit

Permalink
feat: add clean map callback
Browse files Browse the repository at this point in the history
- cleanMapApi optional callback
- update container element type
  • Loading branch information
dqunbp committed Nov 11, 2020
1 parent 7216678 commit 4436a49
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ export default WithoutTokenMap

- **container** - The HTML element `React` `ref` in which Mapbox GL JS will render the map
- **options** *(optional)* - object with native [mapbox-gl](https://docs.mapbox.com/mapbox-gl-js/api/map/#map-parameters) parameters, without container prop
- **setMapApi** *(optional)* - map load callback, called when [mapbox-gl load event](https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:load) is fired
- **setMapApi** *(optional)* - map load callback, called when the [mapbox-gl load event] (https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:load) occurs
- **cleanMapApi** *(optional)* - map cleanup callback, called when the map container is unmounted

```ts
useMapboxGl(
container: React.MutableRefObject<HTMLDivElement>
container: React.MutableRefObject<HTMLElement>
options?: Omit<MapboxOptions, "container">
setMapApi?: (map: mapboxgl.Map) => void
cleanMapAPI?: () => void
)
```

Expand Down
6 changes: 4 additions & 2 deletions src/use-mapbox-gl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export const defaultMapOptions: MapOptions = {
};

export function useMapboxGl(
cotainerRef: React.MutableRefObject<HTMLDivElement>,
cotainerRef: React.MutableRefObject<HTMLElement>,
options?: MapOptions,
setMapAPI?: (map: mapboxgl.Map) => void
setMapAPI?: (map: mapboxgl.Map) => void,
cleanMapAPI?: () => void
) {
useEffect(() => {
const container = cotainerRef.current;
Expand All @@ -28,6 +29,7 @@ export function useMapboxGl(

return () => {
mapboxGlMap.remove();
if (cleanMapAPI) cleanMapAPI();
};
}, []);
}

0 comments on commit 4436a49

Please sign in to comment.