Suj*_*jit 5 javascript reactjs react-native
useEffect(() => {
playLoop();
}, [state.playStatus]);
const playLoop = () => {
if (state.playStatus) {
setTimeout(() => {
console.log("Playing");
playLoop();
}, 2000);
} else {
console.log("Stopped");
return;
}
};
Output:
Stopped
// State Changed to true
Playing
Playing
Playing
Playing
// State Changed to false
Stopped
Playing // This is the problem, even the state is false this still goes on execute the Truthy stalemate
Playing
Playing
Run Code Online (Sandbox Code Playgroud)
我正在研究 react-native,我希望在状态值变为 false 时停止递归。有没有其他方法可以实现这段代码,我只想在状态值为真时重复执行一个函数。谢谢
而不是有一个playStatus布尔值,我会保存间隔 ID。这样,而不是设置playStatus为false,调用clearInterval。同样,不是设置playStatus为true,而是调用setInterval。
// Can't easily use useState here, because you want
// to be able to call clearInterval on the current interval's ID on unmount
// (and not on re-render) (interval ID can't be in an old state closure)
const intervalIdRef = useRef(-1);
const startLoop = () => {
// make sure this is not called while the prior interval is running
// or first call clearInterval(intervalIdRef.current)
intervalIdRef.current = setInterval(
() => { console.log('Playing'); },
2000
);
};
const stopLoop = () => {
clearInterval(intervalIdRef.current);
};
// When component unmounts, clean up the interval:
useEffect(() => stopLoop, []);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
564 次 |
| 最近记录: |