How to avoid constantly calling API? I want to avoid that so that the API won't be stressed when calling
useEffect(
React.useCallback( () => {
const task = InteractionManager.runAfterInteractions( async () => {
const _token = await AsyncStorage.getItem('@token');
let UserId = await AsyncStorage.getItem('@UserID')
setToken(_token);
fetchParents(UserId, _token);
fetchStudent(UserId, _token);
fetchTeacher(UserId, _token);
UserProfile(UserId, _token)
});
return () => task.cancel();
}, [])
);
const fetchParents = async (UserId, _token) => {
try {
fetch(config.API_URL + '/Parents/Application/Access/10/'+UserId, {
method: 'GET',
headers: {
//Headers
'Authorization': 'Bearer '+ _token,
'Content-Type': 'application/json',
},
})
.then((response) => response.json().then(data=>({status:response.status, body:data})) )
.then((response) => {
if(response.status==200)
{
if(response.body.length > 0)
{
setParent(response.body[0].udf_Users_IsHaveApplicationAccess)
}else{
console.log("err");
}
}else{
console.log("error");
}
});
} catch (e) {
console.log(e);
}
}
const fetchStudent = async (UserId, _token) => {
.....//same code in fetchParents
}
const fetchTeacher = async (UserId, _token) => {
.....//same code in fetchParents
}
const UserProfile = async (UserId, _token) => {
.....//same code in fetchParents
}