Map size not invalidated on height / width change
Please make sure to check the following boxes before submitting an issue. Thanks!
- [x] All peer dependencies are installed: React, ReactDOM and Leaflet.
- [x] Using a supported version of React and ReactDOM (v15.x).
- [x] Using the supported version of Leaflet (v1.x) and its corresponding CSS file is loaded.
- [x] Using the latest version of React-Leaflet.
- [x] The issue has not already been reported.
- [x] Make sure you have followed the quick start guide for Leaflet.
- [x] Make sure you have fully read the documentation and that you understand the technical considerations.
Expected behavior
Modifying the height and width props on the element correctly updates the size of the map and the appropriate tiles get loaded in. For example, when programmatically setting the map to be larger, new tiles should be fetched to fill in the newly visible area.
Actual behavior
Modifying the height and width props on the element correctly updates the size of the map, but new tiles are not being loaded until the browser window is physically resized. The missing tiles are simply grey. After searching around, it looks like map.invalidateSize() must be called anytime the height and width are modified. I searched the react-leaflet code and it looks like invalidateSize() is never being called.
This is out of the scope of this library, it's up to your application to handle this.
@PaulLeCam I feel that since react-leaflet is trying to wrap leaflet in a react way, it should update (and do what needs to be done internally) on props change, including dimensions. We should probably not have to touch the leaflet internals for this.
This is out of the scope of this library, it's up to your application to handle this. :-1: 1
I need to express my discontent with this. I second @borisrorsvort opinion that this is indeed a responsibility of this library. Maybe not the whole React community shares these design principles, but I'm 100% sure I'm not alone in thinking that using refs is an anti-pattern. React components should try as much as possible to maintain the declarative philosophy of React.
after dragging for while height and width of map automatically changing.why so?
I just found this and implemented a solution here: https://github.com/PaulLeCam/react-leaflet/pull/147#issuecomment-527939233
The important bit beeing the usage of ref and 'react-resize-detector':
import { withResizeDetector } from 'react-resize-detector'
...
const MyMap = (...) => {
// NOTE: this is using the internal leaflet element to update the size
const mapRef = useRef()
useEffect(() => {
mapRef.current.leafletElement.invalidateSize()
}, [width, height])
return (
<Map
ref={mapRef}
height={height}
width={width}
...
>
)
}
export default withResizeDetector(MyMap)
For those, attempting to correctly update the size of the map after container height and width change. Here's the approach I implemented.
const setMap = ( map: LeafletMap ) => {
const resizeObserver = new ResizeObserver( () => {
map.invalidateSize()
}
const container = document.getElementById('map-container')
resizeObserver.observe(container)
}
<MapContainer
...other properties
id='map-container'
whenCreated={setMap}
>
</MapContainer>
I got the idea of using ResizeObersver from Michael MacFadden's StackOverflow answer
Width is working perfect with the solution of @kjellski, but the height is staying the same after resize. Can somebody help me?
My solution:
const resizeMap = (mapRef: RefObject<Map>) => {
const resizeObserver = new ResizeObserver(() => mapRef.current?.invalidateSize())
const container = document.getElementById('map-container')
if (container) {
resizeObserver.observe(container)
}
}
const BasicMapView = (props: IBasicMapView) => {
const mapRef = useRef<Map>(null)
return (
<MapContainer
ref={mapRef}
id="map-container"
whenReady={() => resizeMap(mapRef)}
>
{/* your map stuff */}
</MapContainer>
)
}
Since the underlying Leaflet.js isn't strongly typed, it's a little risky to assume what whenReady returns in it's function signature.
https://github.com/PaulLeCam/react-leaflet/blob/master/packages/react-leaflet/src/MapContainer.tsx#L65
And since the opinion of this lib is to stay as close to the underlying JS as possible, it's better to just invoke whenReady with an anonymous function where you can pass your ref to the resize function.
The solution of @FaitAccompli worked great, thanks 🎉!
My map opens in a popup component and I was getting an error when closing the popup, because map.invalidateSize() was still being called. This was the error:
Uncaught TypeError: Cannot read properties of undefined (reading '_leaflet_pos') at getPosition (leaflet-src.js:2563:1)
I solved it by storing the observer in a ref variable, and using useEffect to disconnect it when my component unmounts.
Here is my code:
const MyComponent = props => {
// other code
/** @type {React.MutableRefObject<ResizeObserver>} */
let resizeObserver = useRef(null);
useEffect(() => {
return () => {
// Unmounting map, disconnect observer
resizeObserver && resizeObserver.current.disconnect();
};
}, []);
/**
* @param {LeafletMap} map
*/
const setMap = map => {
resizeObserver.current = new ResizeObserver(() => {
map.invalidateSize();
});
const container = document.getElementById("map-container");
resizeObserver.current.observe(container);
};
return (
<MapContainer
// other map props
id="map-container"
whenCreated={setMap}
>
{/* other code */}
</MapContainer>
);
};
I adapted my solution from this discussion, available here: https://github.com/sitcomlab/iip_commute_dashboard/issues/11#issuecomment-2442002270