how to get redux state in setInterval

1.2k Views Asked by At

I use Redux and react, and in timer after one second get value of redux state. Each time the Timer is executed, the value of the state does not change and it is the initial value .initial value in state is

00:00:00

note: The value in the state is correct, only in the timer the value is wrong and the initial value.

 CheckTheEndOfTheExam = setInterval(() => {
    handleSendWxamDataAfterEndTime();
 }, 60000);
 const handleSendWxamDataAfterEndTime = () => {    
       if (getTimeToAttendTheExamPage == data1.examParents[0].examParent_duration) {
           alert('زمان امتحان تمام شده است!!!');
        }
    }
const mapStateToProps = createStructuredSelector({
    getTimeToAttendTheExamPage:getTimeToAttendTheExamPage,
});
1

There are 1 best solutions below

0
On

You can update the state using setState callback and call handleSendWxamDataAfterEndTime function inside the useEffect hook.

const [state, setState] = useState(0);

 CheckTheEndOfTheExam = setInterval(() => {
    setState(s => s + 1)
 }, 60000);
 
 useEffect(() => 
  {
    if (state > 0)
      handleSendWxamDataAfterEndTime()
  }, [state])