Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
React how to not reinitalize mapbox gl js on page change

I am trying to keep the state of the map, to not rerender/initialize the map every time user returns to the map page.

I tried with context where i skip map initialization when map object is not empty, but then im getting blank map.

useEffect(() => {

const initializeMap = () => {
  const map = new mapboxgl.Map({
    container: mapContainerRef.current,
    style: "",
    center: [lng, lat],
    pitch: 59.49999999999986, // pitch in degrees
    bearing: -136.86837902659892, // bearing in degrees
    zoom: zoom,
  });


  map.on("load", () => {
    setMap(map);

    map.resize();
  });

  console.log("init");
};

if (!map) initializeMap(); }, [map]);

Is this possible? Help much appreciated

EDIT:

Found someone asking the same question: https://github.com/mapbox/mapbox-gl-js/issues/8687

Im still having trouble with the container ref, if anyone know how i can update container with preivious map container that would be helpful. I can get previous container by calling map.getContainer() just have no idea how to set it.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

replacing previous div with context map solves this for me:

map ? document.getElementById("map").replaceWith(map.getContainer()) : "";

// in return 

 <div id={"map-container"}>
        <div
          id={"map"}
          className="map-container"
          ref={(el) => (mapContainerRef.current = el)}
        />

Now the map does not reload when navigating between pages

Context implementation example from someone else: https://codesandbox.io/s/mapbox-react-popups-fd4d4?file=/src/components/Map.js

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda