MCPcopy Index your code
hub / github.com/visgl/react-map-gl / Source

Function Source

modules/react-mapbox/src/components/source.ts:81–139  ·  view source on GitHub ↗
(props: SourceProps)

Source from the content-addressed store, hash-verified

79/* eslint-enable complexity */
80
81export function Source(props: SourceProps) {
82 const map = useContext(MapContext).map.getMap();
83 const propsRef = useRef(props);
84 const [, setStyleLoaded] = useState(0);
85
86 const id = useMemo(() => props.id || `jsx-source-${sourceCounter++}`, []);
87
88 useEffect(() => {
89 if (map) {
90 /* global setTimeout */
91 const forceUpdate = () => setTimeout(() => setStyleLoaded(version => version + 1), 0);
92 map.on('styledata', forceUpdate);
93 forceUpdate();
94
95 return () => {
96 map.off('styledata', forceUpdate);
97 // @ts-ignore
98 if (map.style && map.style._loaded && map.getSource(id)) {
99 // Parent effects are destroyed before child ones, see
100 // https://github.com/facebook/react/issues/16728
101 // Source can only be removed after all child layers are removed
102 const allLayers = map.getStyle()?.layers;
103 if (allLayers) {
104 for (const layer of allLayers) {
105 // @ts-ignore (2339) source does not exist on all layer types
106 if (layer.source === id) {
107 map.removeLayer(layer.id);
108 }
109 }
110 }
111 map.removeSource(id);
112 }
113 };
114 }
115 return undefined;
116 }, [map]);
117
118 // @ts-ignore
119 let source = map && map.style && map.getSource(id);
120 if (source) {
121 updateSource(source, props, propsRef.current);
122 } else {
123 source = createSource(map, id, props);
124 }
125 propsRef.current = props;
126
127 return (
128 (source &&
129 React.Children.map(
130 props.children,
131 child =>
132 child &&
133 cloneElement(child, {
134 source: id
135 })
136 )) ||
137 null
138 );

Callers

nothing calls this directly

Calls 5

forceUpdateFunction · 0.70
updateSourceFunction · 0.70
createSourceFunction · 0.70
getMapMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…