Skip to content

React-leaflet map container size not updating on size change #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
niebl opened this issue Mar 26, 2024 · 2 comments
Open

React-leaflet map container size not updating on size change #11

niebl opened this issue Mar 26, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@niebl
Copy link
Collaborator

niebl commented Mar 26, 2024

When used inside a flex container that prompts it to change size, the react leaflet map container does not correctly update it's own size.

May be related to the following issue, where solutions could be found PaulLeCam/react-leaflet#340

@niebl niebl added the bug Something isn't working label Mar 26, 2024
@pauljonescodes
Copy link

I believe I am also experiencing this. As a stop gap measure, I'm updating the key when the size changes.

@pauljonescodes
Copy link

This is now working for me:

import { useEffect, useRef } from "react";
import { MapContainer } from "react-leaflet";

const MAP_CONTAINER_ID = "map-container";

export const ResizableMapContainer = ({ boundsFromContext }) => {
  const mapRef = useRef(null);
  const resizeObserverRef = useRef(null);

  useEffect(() => {
    return () => {
      resizeObserverRef.current?.disconnect();
    };
  }, []);

  const handleWhenReady = () => {
    resizeObserverRef.current = new ResizeObserver(() =>
      mapRef.current?.invalidateSize()
    );
    const mapContainer = document.getElementById(MAP_CONTAINER_ID);
    if (mapContainer) {
      resizeObserverRef.current.observe(mapContainer);
    }
  };

  return (
    <MapContainer
      ref={mapRef}
      id={MAP_CONTAINER_ID}
      whenReady={handleWhenReady}
    >
      {/* stuff */}
    </MapContainer>
  );
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants