useMapBounds.jsx 631 B

12345678910111213141516171819202122232425
  1. import { useEffect, useRef } from "react";
  2. import { getCountryBounds } from "../domain/geocoding";
  3. const useMapBounds = (mapRef, country) => {
  4. const done = useRef(false);
  5. useEffect(() => {
  6. if (done.current || country === null || country === undefined) {
  7. return;
  8. }
  9. const moveMap = async () => {
  10. const bounds = await getCountryBounds(country);
  11. if (bounds) {
  12. mapRef.current.fitBounds(bounds);
  13. mapRef.current.panToBounds(bounds);
  14. mapRef.current.setZoom(3);
  15. done.current = true;
  16. }
  17. };
  18. moveMap();
  19. }, [country, mapRef]);
  20. };
  21. export default useMapBounds;