It's simple. I'm using Redux to manage my state
I have a setTimeout function in a useEffect function.
The setTimeout has a timeout value of 50000ms.
What I Want The SetTimeout Handler To Do
After 50000ms the setTimeout function checks if an api call response has been recieved yet.
If the response hasn't been received yet, the setTimeout function should reinitiate the api call because then the call would have been deemed as timedout.
What The Callback Handler Is Doing
After 50000ms, the setTimeout handler still reinitiates the api call even though the response has been recieved.
I tried logging the output of the state and then it returned a cached state even though the state was passed to the dependency array of the useEffect function and should have been updated
After the api call has been made, the testDetails.isUpdatingTestDetails state is set to false
I tried out several logics and none of them are working
Logic 1
useEffect(() => {
//Notice how i check if the testDetails is being updated before initiating the setTimeout callback
if (testDetails.isUpdatingTestDetails === true) {
setTimeout(() => {
// Inside the settimeout function the same check is also done.
// even though before 50 seconds the response is being received , the function logs the text simulating the reinitiation of an api call
return testDetails.isUpdatingTestDetails === true &&
console.log("After 50 Seconds You Need To Refetch This Data")
}, 50000);
}
}, [testDetails.isUpdatingTestDetails, testDetails])
Logic 2
useEffect(() => {
setTimeout(() => {
return testDetails.isUpdatingTestDetails === true &&
console.log("After 50 Seconds You Need To Refetch This Data")
}, 50000);
}, [testDetails.isUpdatingTestDetails, testDetails])
None of the logic i've applied above are working.