So, I am using expo and react-navigation to create an android application. I want to call a function every 1 second but also want to clear the interval according to the response. The code is as follows:
import React,{useEffect,useState} from 'react';
const Home = props => {
const [intervalId,setIntervalId]=useState(null);
useEffect(()=>{
let interval = setInterval(fetchData,1000);
setIntervalId(interval);
},[]);
const fetchData = async () => {
const response = await fetch(''); //fetch data from some URL here
const res = await response.json();
if(res.someThing) {
clearInterval(intervalId); //clear interval using id stored in state
//no errors but does not seem to clear the interval
}
};
return (
//JSX
);
};
I don't know why clearInterval is not working here or if I am doing something wrong.