MCPcopy Create free account
hub / github.com/visgl/react-map-gl / Layer

Function Layer

modules/react-mapbox/src/components/layer.ts:85–125  ·  view source on GitHub ↗
(props: LayerProps)

Source from the content-addressed store, hash-verified

83let layerCounter = 0;
84
85export function Layer(props: LayerProps) {
86 const map = useContext(MapContext).map.getMap();
87 const propsRef = useRef(props);
88 const [, setStyleLoaded] = useState(0);
89
90 const id = useMemo(() => props.id || `jsx-layer-${layerCounter++}`, []);
91
92 useEffect(() => {
93 if (map) {
94 const forceUpdate = () => setStyleLoaded(version => version + 1);
95 map.on('styledata', forceUpdate);
96 forceUpdate();
97
98 return () => {
99 map.off('styledata', forceUpdate);
100 // @ts-ignore
101 if (map.style && map.style._loaded && map.getLayer(id)) {
102 map.removeLayer(id);
103 }
104 };
105 }
106 return undefined;
107 }, [map]);
108
109 // @ts-ignore
110 const layer = map && map.style && map.getLayer(id);
111 if (layer) {
112 try {
113 updateLayer(map, id, props, propsRef.current);
114 } catch (error) {
115 console.warn(error); // eslint-disable-line
116 }
117 } else {
118 createLayer(map, id, props);
119 }
120
121 // Store last rendered props
122 propsRef.current = props;
123
124 return null;
125}

Callers

nothing calls this directly

Calls 4

forceUpdateFunction · 0.70
updateLayerFunction · 0.70
createLayerFunction · 0.70
getMapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…