0

I am using react-map-gl Mapbox for map but it seems to make requests to mapbox api every time the component is rendered. How can I prevent this? My component

<ReactMapGL
        mapboxApiAccessToken={accessToken}
        {...viewport}
        onViewportChange={nextViewport => setViewport(nextViewport)}
    >
        {markers(list, chooseHandler)}
</ReactMapGL>
Lokesh Bajracharya
  • 457
  • 2
  • 8
  • 19

2 Answers2

0

I stumbled upon the same problem and could finally solve the issue. There is a property reuseMaps that takes care of this.

https://visgl.github.io/react-map-gl/docs/api-reference/map#reusemaps

If reuseMaps is set to true, when a map component is unmounted, the underlying Map instance is retained in memory. The next time a map component gets mounted, the saved instance is reused.

Edit: I saw that you switched to leaflet anyways. Hope it helps a future developer.

2Up1Down
  • 154
  • 3
0

Thanks for your answer 2Up1Down, it has been very useul.

However, I have noticed that when using reuseMaps, even the map on different pages get retained in memory and are displayed, even though they don't have the same props for example the initialViewState. Then, only when I interact with the Map, the props get updated. But that's not a desirable behaviour since the first thing I see is a map with the previous map props and initial position.

I am looking for a solution where only maps with same props are retained in memory

Zach Bkh
  • 66
  • 5