I have 2 problem with my setInterval.
First is, when I try to update a state from a useState, at each iteration of my setInterval, I'm getting doing a rollback into my first state then pushing the current value.
Second is, do not succeed to do a clearInterval and I'm so on an infinit loop.
Here is my code :
const [coordinates, setCoordinates] = useState<{lat: number, lng: number}[]>([]);
let coord = [{cp: '35001', time: 1000}, {cp: '35510', time: 2000}]
useEffect(() => {
let i = 0
const interval = setInterval(() => {
console.log(i)
console.log(coordinates, 'iciiiiiiiiii')
geoCodeConfig()
Geocode.fromAddress(coord[i].cp).then(
response => {
const {lat, lng} = response.results[0].geometry.location;
setCoordinates([...coordinates, {lat: lat, lng: lng}]);
},
error => {
console.error(error);
}
)
i++
if(i === coord.length) { clearInterval(interval)}
}, 2000)
}, [])
Thank you