GithubHelp home page GithubHelp logo

Comments (6)

usefulthink avatar usefulthink commented on May 22, 2024

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.

yestoall avatar yestoall commented on May 22, 2024

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.

yestoall avatar yestoall commented on May 22, 2024

the console.log is always returnig null

from react-google-maps.

usefulthink avatar usefulthink commented on May 22, 2024

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.

yestoall avatar yestoall commented on May 22, 2024

ohhhh thanks.... i understand now

it works perfect

great work, and great help

from react-google-maps.

usefulthink avatar usefulthink commented on May 22, 2024

You're welcome!

from react-google-maps.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.