Skip to content

Commit

Permalink
feat(marker): 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingkang committed Jan 16, 2024
1 parent ae896d7 commit 3230f8d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
16 changes: 14 additions & 2 deletions src/hooks/usePropsReactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ export function usePropsReactive<
>(props: P, ins: Ins, { setterMap = {}, converterMap = {} }: Options = {}) {
const prevProps = usePrevious(props) as P;

const onInstanceCreated = (instance: Ins) => {
useDeepCompareEffect(() => {
if (ins) {
reactivePropChange(props, true);
}
}, [props]);

useUnmount(() => {
if (ins && 'remove' in ins) {
ins.remove();
}
});

const onInstanceCreated = () => {
reactivePropChange(props, false);
};

Expand All @@ -29,7 +41,7 @@ export function usePropsReactive<

try {
Object.keys(nextProps).forEach((key) => {
/** 忽略事件绑定处理 */
/** 忽略事件 */
if (isFunction(props[key]) && /^on[A-Z]/.test(key)) return;

let willReactive = true;
Expand Down
16 changes: 3 additions & 13 deletions src/marker/marker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,19 @@ import Mapbox from 'mapbox-gl';
import { useGetState, usePortal } from '@pansy/react-hooks';
import { forwardRef, useEffect, useImperativeHandle } from 'react';
import { useMap } from '../hooks/useMap';
import { useReact } from '../hooks/useReact';
import { usePropsReactive } from '../hooks/usePropsReactive';
import { useEvents } from '../hooks/useEvents';
import { allProps, setterMap, converterMap } from './config';
import { MarkerEventMap, MarkerEventList } from './constant';

import type { MarkerProps, EventMapping, PropKey, MapboxMarker, MarkerOptions } from './types';
import type { MarkerProps, PropKey, MapboxMarker, MarkerOptions } from './types';

export const Marker = forwardRef<MapboxMarker, MarkerProps>((props, ref) => {
const { map } = useMap();
const { Portal, container } = usePortal();
const [marker, setMarker, getMarker] = useGetState<MapboxMarker>();

// const { onInstanceCreated } = usePropsReactive(
// props,
// marker!,
// {
// setterMap,
// converterMap
// }
// );

const { onInstanceCreated } = useReact<MarkerProps, MapboxMarker, EventMapping>(props, {
ins: marker,
const { onInstanceCreated } = usePropsReactive(props, marker!, {
setterMap,
converterMap,
});
Expand Down
2 changes: 1 addition & 1 deletion src/marker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface CustomizeEvents {
export type EventMapping = { [T in keyof MarkerEvents]: string };

export interface MarkerProps
extends MarkerOptions,
extends Omit<MarkerOptions, 'element' | 'className'>,
Partial<MarkerEvents>,
Partial<CustomizeEvents> {
className?: string;
Expand Down

0 comments on commit 3230f8d

Please sign in to comment.