This behavior caused me a huge headache, and I've narrowed down the problem. I don't know if this is something for Mapbox to deal with-- perhaps it may make a good caveat in the react + mapbox docs/blog posts.
mapbox-gl-js version:
0.46.0
browser:
chrome
Using React, styled-components, and mapbox-gl:
Changing the styled-component's props causes new class names to be generated, and the mapboxgl-map class is not recreated. Despite this fact, map.resize() still works as expected, and the ref still exists.
Quick-fix solutions include:
https://codesandbox.io/s/z64o54w3jp
Passing a new prop to a styled-components map container (e.g. width, height), and calling resize(), should work.
mapboxgl-map class is destroyed, mapbox is not clever enough to detect the missing class and re-add it. If resize() can detect the new container size and fit appropriately, I would expect it to re-inject the class.
Unfortunately we don't have the capacity to debug issues with third-party frameworks like React. I'll reopen the issue if you're able to provide a minimal test case without frameworks that gets to the root of the problem, proving that it's on the GL JS side.
For those who are running into this because the map controls (zoom, compass) are going wonky when repositioning the map container using Styled Components, the easiest thing you can do is go for quick solution 2:
Add
mapboxgl-mapas a className prop or styled-components attr object.
It's a one-line fix and works every time.
Most helpful comment
Unfortunately we don't have the capacity to debug issues with third-party frameworks like React. I'll reopen the issue if you're able to provide a minimal test case without frameworks that gets to the root of the problem, proving that it's on the GL JS side.