mirror of
https://github.com/visgl/react-map-gl.git
synced 2025-12-08 20:16:02 +00:00
1016 B
1016 B
useMap
The useMap hook, used with the MapProvider, helps an app to perform map operations outside of the component that directly renders a Map.
import {MapProvider, Map, useMap} from 'react-map-gl';
function Root() {
return (
<MapProvider>
<Map id="myMapA" ... />
<Map id="myMapB" ... />
<NavigateButton />
</MapProvider>
);
}
function NavigateButton() {
const {myMapA, myMapB} = useMap();
const onClick = () => {
myMapA.flyTo({center: [-122.4, 37.8]});
myMapB.flyTo({center: [-74, 40.7]});
};
return <button onClick={onClick}>Go</button>;
}
See a full example here.
Signature
useMap(): {[id: string]: MapRef}
The hook returns an object that contains all mounted maps under the closest MapProvider. The keys are each map's id and the values are the ref object.