Skip to content

Commit

Permalink
feat(maeker): 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingkang committed Aug 31, 2022
1 parent 3172880 commit 9f1283e
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/marker/marker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Marker as MapboxMarker } from 'mapbox-gl';
import { useState } from 'react';
import { createPortal } from 'react-dom';
import { useGetState } from '@pansy/react-hooks';
import { forwardRef, useEffect, useRef, useImperativeHandle } from 'react';

import { useMap } from '@/hooks/useMap';
Expand All @@ -14,7 +14,7 @@ import type { MarkerOptions } from 'mapbox-gl';
export const Marker = forwardRef<MapboxMarker, MarkerProps>((props, ref) => {
const map = useMap();
const contentWrapper = useRef<HTMLDivElement>();
const [marker, setMarker] = useGetState<MapboxMarker>(null as any);
const [marker, setMarker] = useState<MapboxMarker>();

const { onInstanceCreated } = useReact<MarkerProps, MapboxMarker, EventMapping>(props, {
ins: marker,
Expand Down Expand Up @@ -44,10 +44,6 @@ export const Marker = forwardRef<MapboxMarker, MarkerProps>((props, ref) => {

const marker = new MapboxMarker(contentWrapper.current, options);

marker.setLngLat(props.lngLat);

marker.addTo(map);

return Promise.resolve(marker);
};

Expand All @@ -65,5 +61,5 @@ export const Marker = forwardRef<MapboxMarker, MarkerProps>((props, ref) => {
return options;
};

return marker && createPortal(props.children, marker.getElement());
return <>{marker && createPortal(props.children, marker.getElement())}</>;
});

0 comments on commit 9f1283e

Please sign in to comment.