I started a timer and want to stop it if the state of isPaying changes to false
const [isPlaying, setIsPlaying] = useState(false)
const isPlayingRef = useRef(isPlaying);
const play = () => {
setIsPlaying(true);
const interval = setInterval(() => {
if(!isPlaying){
clearInterval(interval);
}
console.log('isPlaying ', isPlaying);
console.log('isPlaying ', isPlayingRef.current);
}, 5000);
}
Both isPlaying
and isPlayingRef.current
are false inside the timer despite that I am setting the value to true in the play function.