Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't set lat, lng in state

I get the lat and lng when the user clicks on the map, and I console.log them. This works fine, but after when I would like to set them in a state I get this error:
enter image description here

I don’t know how could this occur, since both were defined, and worked just two lines ago.

The necessary code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const [userPickPos, setUserPickPos] = useState()
  const MapEvents = () => {
    useMapEvents({
      click(e) {
        const lat = e.latlng.lat;
        console.log(lat) //works fine
        const lng = e.latlng.lng;
        console.log(lng) //works fine

        setUserPickPos(lat, lng) //gives me the error

      },
    });
}

>Solution :

you should pass the whole object to useState defined setter:

setUserPickPos({lat: lat, lng: lng})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading