In the code below, when the app loads initially "Location changed offline!" is logged every time the location updates. When online is set to true with a TouchableOpacity, the message logged to the console looks like this:
LOG true
LOG attempting to update location...
LOG Location updated in real-time!
LOG false
LOG Location changed offline!
LOG true
LOG attempting to update location...
LOG true
LOG attempting to update location...
LOG Location updated in real-time!
For some reason it's randomly changing the state of online back to false, thus causing the "Location changed offline!" to be logged. What could be causing this?
const [online, setOnline] = useState(false);
useEffect(() => {
Geolocation.watchPosition(
position => {
console.log(online);
if (online) {
console.log('attempting to update location...');
const payload = {
lat: position.coords.latitude,
lng: position.coords.longitude,
id: 1,
};
axios
.post('http://localhost:3000/location/update', payload)
.then(res => {
if (res.status === 200) {
console.log('Location updated in real-time!');
return;
}
})
.catch(err => console.log(err.response));
} else {
console.log('Location changed offline!');
}
},
err => console.log(err.response)
);
}, [online]);