I created my project React-Native. My project use 'expo-location' for tracking location module. I use setInterval function (inside useEffect hooks) for get last location every 10 seconds. But after I tested app, there is warning message below..
From warning message, I think the problem is come from setInterval function. But I try to fix but it cannot pass, because I maybe use wrong coding.
Problem component is below,
import React, { useState, useEffect, useRef } from 'react';
import Constants from 'expo-constants';
import * as Location from 'expo-location';
import io from 'socket.io-client';
import BasedModel from '../models/BasedModel';
const objBasedModel = new BasedModel;
const Tracker = () => {
const ref = useRef();
const [trigger, setTrigger] = useState(Date.now());
const [trackLocation, setTrackLocation] = useState();
const [errorMsg, setErrorMsg] = useState('');
const submitLocation = (location) => {
const sendMessage = {
"latitude": location.coords.latitude,
"longitude": location.coords.longitude
};
ref.current.emit('emit location', sendMessage);
}
useEffect(() => {
const socket = io(objBasedModel.serverUrl);
ref.current = socket;
(async () => {
if (Platform.OS === 'android' && !Constants.isDevice) {
setErrorMsg(
'Oops, this will not work on Snack in an Android emulator. Try it on your device!'
);
return;
}
let { status } = await Location.requestPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
console.log("location: ", location);
setTrackLocation(location);
setInterval(() => {
submitLocation(location);
setTrigger(Date.now());
}, 10000);
})();
return () => socket.disconnect();
}, []);
return (
<React.Fragment />
);
}
export default Tracker;
How can I solve the warning "Warning: Can't perform a React state update on an unmounted component." from setInterval function by the right way?
Thanks in advance,
