Skip to content

Commit

Permalink
provide access to created mapboxgl.Marker
Browse files Browse the repository at this point in the history
  • Loading branch information
yoiang committed Feb 24, 2022
1 parent 3432caf commit c4f34bc
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/components/mapboxgl/Marker.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mapboxgl from 'mapbox-gl'
import React, {
RefObject,
useCallback,
useContext,
useEffect,
Expand All @@ -19,6 +20,8 @@ export interface Props {
popup?: React.ReactElement | undefined
// eslint-disable-next-line no-unused-vars
onClick?: (event: MouseEvent) => void
mapboxMarkerRef?: (marker: RefObject<mapboxgl.Marker | undefined>) => void

children?: React.ReactElement | undefined
}

Expand All @@ -28,7 +31,13 @@ export interface MarkerMethods {
getMapboxMarker: () => React.MutableRefObject<mapboxgl.Marker>
}

export const Marker = ({ location, popup, onClick, children }: Props) => {
export const Marker = ({
location,
popup,
onClick,
mapboxMarkerRef,
children
}: Props) => {
const context = useContext(MapContext)
const { map } = context

Expand Down Expand Up @@ -81,6 +90,9 @@ export const Marker = ({ location, popup, onClick, children }: Props) => {
}
}

if (mapboxMarkerRef) {
mapboxMarkerRef(mapboxMarker)
}
return () => {
if (!mapboxMarker.current) {
return
Expand Down

0 comments on commit c4f34bc

Please sign in to comment.