Skip to content
Advertisement

Same array inside of useEffect and getCoordinates is different when console.log

Using GoogleMap API to display custom locations

I have an array of json object called data imported; it has a property called address. Google map api looks up coordinates of the address property and should generates custom Markers from an array called locations.

But the value of locations inside fetchLocations() and getCoordinates, are different when I console.log, although the both represent the same variable.

What did I do wrong?

JavaScript

Advertisement

Answer

The locations array should never be changed in any way. It’s supposed to be immutable. The only thing that is able to change it is the setLocations function.

Instead of locations.push(position) do:

JavaScript

Also, getCoordinates is not returning anything. It should return the same value sent to setLocations:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement