fix: useEffect nur einmal ausführen "POI hinzufügen" war manchmal zweimal oder mehr vorhanden im Kontextmenü

This commit is contained in:
ISA
2024-06-06 08:38:52 +02:00
parent 29bf479101
commit 3d72b3682f

View File

@@ -48,6 +48,7 @@ const plusRoundIcon = L.icon({
});
const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
const [menuItemAdded, setMenuItemAdded] = useState(false);
const linePositions = lineCoordinates || [
[52.505, 8],
[52, 8.5],
@@ -1135,7 +1136,9 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
}).addTo(map);
}
}, [map]); */
const addItemsToMapContextMenu = () => {
if (!menuItemAdded) {
console.log("contextMenuItems hasRights:", hasRights);
map.contextmenu.addItem({
@@ -1144,19 +1147,20 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
className: "background-red",
callback: (event) => addStationCallback(event, hasRights),
});
setMenuItemAdded(true); // Menüpunkt wurde hinzugefült, Zustand aktualisieren
}
};
//--------------------------------------------------
useEffect(() => {
if (map && poiLayerRef.current && isPoiTypLoaded) {
if (map && poiLayerRef.current && isPoiTypLoaded && !menuItemAdded) {
addItemsToMapContextMenu();
}
}, [
map,
locations,
onLocationUpdate,
poiReadTrigger,
poiLayerRef,
isPoiTypLoaded,
userRights,
menuItemAdded, // Hinzufügen zu den Abhängigkeiten, um den Effekt korrekt zu steuern
]);
//------------------------------------------