Upgrade Guide
Upgrading to v8.0
-
All imports from
react-map-glshould be replaced with one of the following endpoints:- If using with
mapbox-gl@>=3.5.0: import fromreact-map-gl/mapbox - If using with
mapbox-gl@<3.5.0: import fromreact-map-gl/mapbox-legacy
- If using with
-
maplibre-gl@<=3is no longer supported. -
Some TypeScript types have been renamed to align with the official types from the base map libraries:
Old name New name MapStyleStyleSpecificationFogFogSpecificationLightLightSpecificationTerrainTerrainSpecificationProjectionProjectionSpecification*Layer*LayerSpecification*SourceRaw*SourceSpecification
MapLibre
Removed default for RTLTextPlugin
The default RTLTextPlugin loaded from mapbox.com has been removed to align with the default behavior of MapLibre.
To keep the previous behavior, specify the pluginUrl which was previously used or supply the plugin from any other source:
<Map RTLTextPlugin="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.2.3/mapbox-gl-rtl-text.js" />
Upgrading to v7.1
maplibre-glusers no longer need to installmapbox-glor a placeholder package as dependency. Change your imports to the new endpointreact-map-gl/maplibre. Components imported from here do not require setting themapLibprop, and use the types defined bymaplibre-gl.
import Map from 'react-map-gl';
import maplibregl from 'maplibre-gl';
function App() {
return <Map
mapLib={maplibregl}
style={MAP_STYLE}
maplibreLogo // This will generate a TypeScript error because it's not defined in Mapbox options
/>;
}
import Map from 'react-map-gl/maplibre'; // <- mind the updated import
function App() {
return <Map
// mapLib is default to `import('maplibre-gl')`
style={MAP_STYLE}
maplibreLogo
/>
}
If you installed mapbox-gl from a placeholder such as npm:empty-npm-package@^1.0.0 as suggested by the previous version's documentation, it should be removed from your package.json.
- The
@types/mapbox-gldependency has relaxed its version constraint. If you usemapbox-glas the base map library, it's recommended to explicitly list@types/mapbox-glin your package.json with a version matching that ofmapbox-gl(v1 or v2). This package is no longer required by the non-mapbox code path, and may be further demoted to an optional peer dependency in a future release. - If you use the
Mapcomponent as a child of theDeckGLcomponent fromdeck.gl, upgradedeck.glto>=8.9.18.
Upgrading to v7.0
v7 is a complete rewrite of the library. It is redesigned to be fast, lightweight, fully typed, to behave the same and expose the same APIs as the wrapped map library, and to provide the maximum compatibility with third-party plugins. To take advantages of these new features, you need to make some changes to your code that was previously depending on react-map-gl v5 and v6.
If you are using react-map-gl controls (
Marker,Popup,NavigationControletc.) with deck.gl'sContextProvider, do not upgrade to this version. The old approach no longer works with v7. We are moving the support for this use case to a new project that does not depend on mapbox.
Dependencies
- Add
mapbox-gl(or a compatible fork) to your package.json.react-map-glno longer lists a specific map renderer in its dependencies, so you are free to use it with Mapbox v1, v2 or Maplibre. viewport-mercator-project(an alias of@math.gl/web-mercator) is no longer a dependency. You can still install the library on the side as a utility for viewport-related math, but it's no longer required.
Module exports
InteractiveMapandStaticMapare removed. Instead, importMap.setRTLTextPluginis removed. Use theMapcomponent'sRTLTextPluginprop (default enabled).MapControlleris removed. v7.0 has removed its own implementation of user input handling in favor of the native handlers. If you are using a custom implementation ofMapController, check if the native handlers offer options to address your application's needs.MapContextanduseMapControlare removed. Check out the new API useMap and useControl.- The overlay components (
HTMLOverlay,CanvasOverlayandSVGOverlay) are removed. Check out this example for implementing similar controls in your own application. LinearInterpolatorandFlyToInterpolatorare removed. Usemap.easeTo()andmap.flyTo()instead, see this example.
Map
- Renamed props for better consistency with the wrapped library:
mapboxApiAccessTokenis nowmapboxAccessTokenmapboxApiUrlis nowbaseApiUrlpreventStyleDiffing(defaultfalse) is replaced withstyleDiffing(defaulttrue)
mapStyleshould be explicitly specified. The default value has changed from"mapbox://styles/mapbox/light-v9"to an empty style.- The following props are removed and apps should use
styleinstead:widthheightvisible
onViewportChange,onViewStateChangeandonInteractionStateChangeare removed. You can either useMapas an uncontrolled component with the newinitialViewStateprop, or if you need to manage the camera state externally (e.g. in Redux), use theonMovecallback instead. See examples in state management.transition*props are removed. Usemap.easeTo()andmap.flyTo()instead, see this example.mapOptionsis removed. Almost all of the options for the nativeMapclass are exposed as props.onHoveris removed. UseonMouseMoveoronMouseEnter.- The event argument is changed for all interaction callbacks. See documentation for details.
getCursoris removed as part of the effort to getMapbehave the same as the native component. To set the cursor, use thecursorprop. Follow this example to change cursor on hover.touchActionandeventRecognizerOptionsare removed. Check out thecooperativeGesturesprop.
Other components
capture*props are removed.*labelprops are removed. UseMap'slocaleprop.- All map controls' props are now strictly aligned with their mapbox-gl counterparts. In heading this direction, we are able to remove a significant amount of custom code and have the components behave more predictably for developers switching from the native library. If your application is relying on an old feature that is no longer supported, please open a topic on Discussion so we can review on a case-by-case basis.
Upgrading to v5.3/v6.1
MapContextis now an official API. The experimental_MapContextexport will be removed in a future release.react-virtualized-auto-sizeris no longer a dependency.- Inertia has been enabled by default on the map controller. To revert to the behavior in previous versions, set the interaction options:
const CONTROLLER_OPTS = {
dragPan: {inertia: 0},
dragRotate: {inertia: 0},
touchZoom: {inertia: 0}
};
<MapGL {...CONTROLLER_OPTS} ... />
SourceandLayercomponents no longer expose imperative methods viarefas part of the migration to functional components. This is to comply with the pattern recommended by the latest React.- If you used to call
sourceRef.getSource(), it can be replaced withmapRef().getMap().getSource(sourceId). - If you used to call
layerRef.getLayer(), it can be replaced withmapRef().getMap().getLayer(layerId).
- If you used to call
Upgrading to v6
- A valid Mapbox access token is always required.
- The default value of
InteractiveMap'smaxPitchprop is changed to85from60. mapbox-glv2 introduced a breaking change to the build system. Transpiling it may result in a crash in the production build with the messagem is not defined. Find solutions in this thread.
Upgrading to v4
onChangeViewportis removed, useonViewportChangeinsteadImmutable.jsis no longer a dependency- Export
experimental.MapControlsis removed, useMapControllerinstead InteractiveMap'smapControlsprop is renamed tocontroller- Removed support for the deprecated
interactiveproperty on the layer styles. Use theinteractiveLayerIdsprop to specify which layers are clickable.
Upgrading to v3.2
- The latest mapbox-gl release requires stylesheet to be included at all times. See Get Started for information about styling.
- Immutable.js is no longer a hard dependency and will be removed in the next major release. If you are importing immutable in your application, it is recommended that you explicitly list it in the application's dependencies.
Upgrading to v3
v3 is a major upgrade of react-map-gl. While we have tried to gently deprecated any changed or removed features, a few breaking changes could not be avoided.
Version Requirements
- The Node Version Requirement for building react-map-gl is now >=v6.4.0. Using prebuilt react-map-gl does NOT has this limitation. This is introduced by Mapbox GL JS v0.38.0
MapGL Component
- Two Map Components - v3 now splits the Map component into two React components:
StaticMapandInteractiveMap.InteractiveMapis the default export, and designed to be as compatible as possible with the v2 default component.
onChangeViewport callback now includes width and height.
The viewport parameter passed to the onChangeViewport callback now includes width and height. Application code that composed the viewport with width and height may have to be updated. Please double check your render code if you relied on this behavior.
// BAD: 'width' and 'height' below will be overridden by what's in the 'viewport' object
<ReactMapGL width={500} height={400} {...viewport} />
// GOOD: 'width' and 'height' below will override the values in 'viewport'
<ReactMapGL {...viewport} width={500} height={400} />
Overlays
- Some Overlays Moved to Examples - Some less frequently used overlays (
DraggablePointsOverlay,ChoroplethOverlay,ScatterplotOverlay), have been moved to examples. Most users are now using mapbox styles or deck.gl layers and removing these overlays reduces the size of the react-map-gl library for the majority of users that don't need them. If you still use them, simply copy the overlay source file(s) into your application. - Overlays must be Children of the Map - Overlays must now be rendered as children of the main
react-map-glcomponent to automatically sync with the map viewport.
fitBounds utility function
The fitBounds utility has been moved to the math.gl library. The function can now be called as follows:
import WebMercatorViewport from 'viewport-mercator-project';
const viewport = new WebMercatorViewport({width: 600, height: 400});
const bound = viewport.fitBounds(
[[-73.9876, 40.7661], [-72.9876, 41.7661]],
{padding: 20, offset: [0, -40]}
);
// => bounds: instance of WebMercatorViewport
// {longitude: -73.48760000000007, latitude: 41.268014439447484, zoom: 7.209231188444142}
Deprecations
We have started to deprecate a few React props. In all the cases below, the old props will still work (you'll get a warning in the console), but they will likely be removed in the next major version of react-map-gl so you should start using the new props as soon as possible.
| Old Prop | New Prop |
|---|---|
onChangeViewport(<viewport>) | onViewportChange(<viewport>) |
onHoverFeatures(<features>) | onHover(<event>) |
onClickFeatures(<features>) | onClick(<event>) |
perspectiveEnabled [default: false] | dragRotate [default: true] |
Upgrading to v2
v2 is API compatible with v1, however if you are still using v1 of react-map-gl, make sure that you first upgrade:
- Your
nodeversion tov4or higher - Your
reactversion to15.4or higher.
Background: mapbox-gl 0.31.0 introduced a hard dependency on Node >= v4.
Upgrading to v1
(Upgrading from 0.6.x)
- Importing Overlays - The map overlay components (
HTMLOverlay,CanvasOverlay,SVGOverlayetc) are now named exports. They previously had to be imported via their relative source paths:
// v1.0
import MapGL, {SVGOverlay} from 'react-map-gl';
// v0.6
import MapGL from 'react-map-gl';
import SVGOverlay from 'react-map-gl/src/api-reference/svg-overlay';.
- Map State - The map state reported by
onViewportChangedwill now contain additional state fields (tracking not onlypitchandbearingneeded for perspective mode, but also transient information about how the projection is being changed by the user).- This information must be passed back to the react-map-gl component in the next render.
- To simplify and future proof applications, it is recommended to simply save the entire
mapStatein your app store whenever it changes and then pass it back to the component rather than trying to keep track of individual fields (likelongitude,latitudeandzoom).