I'm trying to send get requests at certain interval using setInterval. So my script looks like that:
const [package_id, setPackageId] = useState({id: 1, result: NaN})
async function get_images(detail_id){
axios.get('http://127.0.0.1:8000/get_images', {params: {detail_id}, headers:{
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': '0',
}}).then((res) =>{
... // some processing
setPackageId(prevstate => {return {...prevstate, id :prevstate.id + 1}})
}
).catch(errs => {
console.log(errs)
})
}
useEffect(() => {
const request_interval = setInterval(get_images, 2000, package_id.id)
return () => clearInterval(request_interval);
}, [])
Even though state package_id.id is being updated(I checked using useEffect). In get_images function, it's value is always the same. How can I use updated value of state?
PS: I also tried to access state inside of function(without passing argument (detail_id)), but it didn't work either.
Any help would be appreciated.