Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda