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

386
Vistas
How to call useMap() outside of the file where <MapContainer> is NOT called?

I am using react-leaflet.

For example I want to set bounds to map outside of the file where <MapContainer> is called. For this I need to use const map = useMap();. How do I connect useMap() with the file where was called?

Or to clarify even more.. I want to connect with the map outside of the map file to be enable to add/remove stuff. For example I want to enable markers outside of the file where <MapContainer> is called.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

ghybs is probably right in that you're probably thinking of doing something that doesn't truly require the L.map object in an ancestor of the MapContainer. Especially if you're simply talking about using map Marker components - theyre only ever useful as children of a map. That being said, what you're talking about doing is simple to set up. An example of how to make the underlying L.map object available is right there in the react-leaflet docs.

A quick example:

const App = () => {
  const [mapRef, setMapref] = useState();
  return (
    <>
      <Map setMapRef={setMapRef} />
      <SomewhereElse mapRef={mapRef} />
    </>
  )
}

const Map = ({ setMapRef }) => {
  return <MapContainer whenCreated={setMapRef} />
}

Once the MapContainer loads the leafletmap, whenCreated is fired, which then fires the callback setMapRef. The L.map instance is now saved to the mapRef state variable in App, and is available there to be used SomewhereElse.

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