fixed code
This commit is contained in:
@@ -1,22 +1,30 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
|
||||
const YANDEX_GEOCODER_API_KEY = import.meta.env.VITE_YANDEX_GEOCODER_KEY;
|
||||
|
||||
export function useYandexMap(enabled, initialCoords = null, initialAddress = "") {
|
||||
const mapRef = useRef(null);
|
||||
const ymapRef = useRef(null);
|
||||
const placemarkRef = useRef(null);
|
||||
const initializedRef = useRef(false); // защита от двойного вызова StrictMode
|
||||
const requestIdRef = useRef(0); // защита от гонки при быстрых кликах/драгах
|
||||
|
||||
const [coords, setCoords] = useState(initialCoords);
|
||||
const [address, setAddress] = useState(initialAddress ?? "");
|
||||
const addressRef = useRef(initialAddress ?? ""); // ← добавьте
|
||||
const addressRef = useRef(initialAddress ?? "");
|
||||
|
||||
const resolveAddress = useCallback((lat, lng) => {
|
||||
const requestId = ++requestIdRef.current;
|
||||
|
||||
const resolveAddress = (lat, lng) => {
|
||||
fetch(
|
||||
`https://geocode-maps.yandex.ru/1.x/?apikey=c2d3c717-a109-4494-8863-7edbc32044e3&geocode=${lng},${lat}&format=json&lang=ru_RU`
|
||||
`https://geocode-maps.yandex.ru/1.x/?apikey=${YANDEX_GEOCODER_API_KEY}&geocode=${lng},${lat}&format=json&lang=ru_RU`
|
||||
)
|
||||
.then(r => r.json())
|
||||
.then(data => {
|
||||
// Игнорируем ответ, если пришёл более новый запрос — иначе
|
||||
// устаревший ответ может перезаписать адрес актуальных координат.
|
||||
if (requestId !== requestIdRef.current) return;
|
||||
|
||||
const found =
|
||||
data?.response?.GeoObjectCollection?.featureMember?.[0]
|
||||
?.GeoObject?.metaDataProperty?.GeocoderMetaData?.text;
|
||||
@@ -25,11 +33,13 @@ export function useYandexMap(enabled, initialCoords = null, initialAddress = "")
|
||||
addressRef.current = resolved;
|
||||
})
|
||||
.catch(() => {
|
||||
if (requestId !== requestIdRef.current) return;
|
||||
|
||||
const fallback = `${lat.toFixed(5)}, ${lng.toFixed(5)}`;
|
||||
setAddress(fallback);
|
||||
addressRef.current = fallback;
|
||||
});
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled || !mapRef.current) return;
|
||||
@@ -37,7 +47,6 @@ export function useYandexMap(enabled, initialCoords = null, initialAddress = "")
|
||||
initializedRef.current = true;
|
||||
|
||||
const init = () => {
|
||||
// Если карта уже создана — не создаём снова
|
||||
if (ymapRef.current) return;
|
||||
|
||||
const center = initialCoords
|
||||
@@ -90,13 +99,12 @@ export function useYandexMap(enabled, initialCoords = null, initialAddress = "")
|
||||
}
|
||||
|
||||
return () => {
|
||||
// Уничтожаем карту только при размонтировании компонента
|
||||
ymapRef.current?.destroy();
|
||||
ymapRef.current = null;
|
||||
placemarkRef.current = null;
|
||||
initializedRef.current = false;
|
||||
};
|
||||
}, [enabled]);
|
||||
}, [enabled, initialCoords, resolveAddress]);
|
||||
|
||||
return { mapRef, coords, address, addressRef };
|
||||
}
|
||||
Reference in New Issue
Block a user