11import * as React from 'react' ;
2- import { useContext , useEffect , useMemo , useCallback , useState , useRef , cloneElement } from 'react' ;
2+ import { useContext , useEffect , useMemo , useState , useRef , cloneElement } from 'react' ;
33import { MapContext } from './map' ;
44import assert from '../utils/assert' ;
55import { deepEqual } from '../utils/deep-equal' ;
@@ -25,7 +25,8 @@ export type SourceProps = (SourceSpecification | CanvasSourceSpecification) & {
2525let sourceCounter = 0 ;
2626
2727function createSource ( map : MapInstance , id : string , props : SourceProps ) {
28- if ( map . isStyleLoaded ( ) ) {
28+ // @ts -ignore
29+ if ( map . style && map . style . _loaded ) {
2930 const options = { ...props } ;
3031 delete options . id ;
3132 delete options . children ;
@@ -83,12 +84,11 @@ export function Source(props: SourceProps) {
8384 const [ , setStyleLoaded ] = useState ( 0 ) ;
8485
8586 const id = useMemo ( ( ) => props . id || `jsx-source-${ sourceCounter ++ } ` , [ ] ) ;
86- const forceUpdate = useCallback ( ( ) => setStyleLoaded ( version => version + 1 ) , [ ] ) ;
8787
8888 useEffect ( ( ) => {
8989 if ( map ) {
90- // Fired on initial load signaling the map is ready to add custom sources
91- // Subsequently fired on style changes
90+ /* global setTimeout */
91+ const forceUpdate = ( ) => setTimeout ( ( ) => setStyleLoaded ( version => version + 1 ) , 0 ) ;
9292 map . on ( 'styledata' , forceUpdate ) ;
9393 forceUpdate ( ) ;
9494
@@ -124,20 +124,6 @@ export function Source(props: SourceProps) {
124124 }
125125 propsRef . current = props ;
126126
127- useEffect ( ( ) => {
128- if ( ! source ) {
129- // on `styledata` event, `map.isStyleLoaded()` still returns false.
130- // `load` and `style.load` only fire once and not when `isStyleLoaded` changes from true to false to true.
131- // `sourcedata` potentially suggests that `isStyleLoaded` has changed. But it fires on every tile load.
132- // Unsubscribe once source is added.
133- map . on ( 'sourcedata' , forceUpdate ) ;
134- return ( ) => {
135- map . off ( 'sourcedata' , forceUpdate ) ;
136- } ;
137- }
138- return undefined ;
139- } , [ map , source ] ) ;
140-
141127 return (
142128 ( source &&
143129 React . Children . map (
0 commit comments