Skip to content

Commit

Permalink
fix(Source): 修复 Source 没有创建
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingkang committed Jan 17, 2024
1 parent 28fb0d9 commit 83801a2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/source/source.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState, useRef, useMemo } from 'react';
import React, { useEffect, useState, useRef, useMemo, cloneElement } from 'react';
import { useMap } from '../hooks/useMap';
import { isStyleLoaded } from '../utils/isStyleLoaded';
import { updateSource, createSource } from './utils';

import type { SourceProps } from './types';
Expand All @@ -22,7 +23,7 @@ export function Source(props: SourceProps) {
return () => {
map.off('styledata', forceUpdate);

if (map.getStyle() && map.isStyleLoaded() && map.getSource(id)) {
if (map.getStyle() && isStyleLoaded(map) && map.getSource(id)) {
const allLayers = map.getStyle()?.layers;

if (allLayers) {
Expand Down Expand Up @@ -50,5 +51,16 @@ export function Source(props: SourceProps) {
}
propsRef.current = props;

return null;
return (
(source &&
React.Children.map(
props.children,
(child) =>
child &&
cloneElement(child as any, {
source: id,
}),
)) ||
null
);
}
3 changes: 2 additions & 1 deletion src/source/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from '../utils/assert';
import { isStyleLoaded } from '../utils/isStyleLoaded';
import { deepEqual } from '../utils/deepEqual';

import type {
Expand All @@ -17,7 +18,7 @@ export function createSource<SourceT extends Source>(
id: string,
props: SourceProps<SourceT>,
) {
if (map.isStyleLoaded()) {
if (isStyleLoaded(map)) {
const options = { ...props };
delete options.id;
delete options.children;
Expand Down
6 changes: 6 additions & 0 deletions src/utils/isStyleLoaded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Map } from 'mapbox-gl';

export const isStyleLoaded = (map: Map): boolean => {
// @ts-ignore
return map.style && map.style._loaded;
};

0 comments on commit 83801a2

Please sign in to comment.