Comments (6)
Do you have a code-snippet for me? useMap
returns null if it is used outside the APIProvider
component (since it can't lookup map-instances in that case) or when the map with the specified id can't be found (currently, not specifying an id will use the id "default"
, which is also used by the map if you don't specify the id
prop).
See: https://github.com/visgl/react-google-maps/blob/main/src/hooks/use-map.ts
from react-google-maps.
i'm using it inside de APIProvider
import { useEffect } from "react"
import { APIProvider, Map, useMap } from "@vis.gl/react-google-maps"
export default function Mapa() {
const mapa = useMap()
useEffect(() => {
console.log("mapa *", mapa)
}, [mapa])
// ...
return (
<APIProvider apiKey="...mykey">
<Map
mapId="rutas"
mapTypeId="terrain"
...
></Map>
</APIProvider>
)
}
from react-google-maps.
the console.log is always returnig null
from react-google-maps.
Here's the problem: useMap
isn't actually called inside the APIProvider
, the APIProvider is rendered as a child of your Mapa
component.
I would recommend to place the APIProvider outside of this component, could be as far up as your App
or page component. It doesn't impact the rest of the application in any way.
The alternative would be to place the component using the hook into a child-component like this:
function SomethingWithMapHook() {
const map = useMap();
useEffect(() =>{
if (!map) return;
// whatever you need to do with the map
}, [map])
return <>whatever</>;
}
export default function Mapa() {
const mapa = useMap()
useEffect(() => {
console.log("mapa *", mapa)
}, [mapa])
// ...
return (
<APIProvider apiKey="...">
<Map {...mapProps}>
...
</Map>
<SomethingWithMapHook />
</APIProvider>
)
}
from react-google-maps.
ohhhh thanks.... i understand now
it works perfect
great work, and great help
from react-google-maps.
You're welcome!
from react-google-maps.
Related Issues (20)
- [Feat] Implement `<Polyline>` component. HOT 14
- Support for typescript types HOT 4
- [Bug] InfoWindow renders twice after content load HOT 4
- [Bug] setting center via onCenterChanged on Map causes the map to move without the markers HOT 1
- [Feat] `useMap` should log an error when used outside the APIProvider context
- [Bug] Map will move when executing onClick function of Marker component HOT 1
- [Bug] Defining the optional autocomplete options caused a useEffect error HOT 2
- Can't import the named export 'Children' from non EcmaScript module (only default export is available) HOT 5
- [Bug] Some ControlPositions are causing my component to disappear HOT 2
- [Bug] Race Condition during event handlers initialization HOT 1
- [Bug] Invalid/Expired API key crashes the page HOT 4
- [Bug] Adding more than one class to AdvancedMarkers className throws an error
- onClick on AdvancedMarker to change Map zoom changes only once [Bug]
- [Bug] Routes API not enabled for our production API-Key
- [Feat] ImageMapType Support HOT 1
- [Feat] Adding missing components from @googlemaps/extended-component-library such as split-layout HOT 2
- [Bug] changing the center coordinates doesn't reposition the map HOT 8
- [Bug] You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors HOT 4
- [Feat] Allow `<Pin>` glyphs to be passed as children HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-google-maps.