Agontuk / react-native-geolocation-service

React native geolocation service for iOS and android
https://www.npmjs.com/package/react-native-geolocation-service
MIT License
1.61k stars 292 forks source link

Problem when I try to save coords in Array using useState #256

Closed luksrocha closed 3 years ago

luksrocha commented 3 years ago

I'm getting the coords and saving it into an array, but, even using spread operator I get only one item into it.

`

const getLocation = () => {Geolocation.watchPosition(
  (position) => {
    console.log('Array value before save', coords)
    const { latitude, longitude } = position.coords
    setCoords([...coords, latitude + ',' + longitude]);
  },
  (error) => {
    console.log(error);
  },
  {
    fastestInterval: 2500,
    interval: 2500,
    distanceFilter: 1
  })}

` and that is what I get

image

I need to save all of coords that I get in my Geolocation.watchPosition(), what should I do?

danilaberentsev commented 3 years ago

try to use callback syntax setCoords(prevState => ([...prevState, ${latitude} ${longitude}]))

luksrocha commented 3 years ago

try to use callback syntax setCoords(prevState => ([...prevState, ${latitude} ${longitude}]))

It worked for me!! thank you