Javascript 反应本机清除间隔不停止间隔
当我离开组件时,ticker从不停止并抛出“无法对未安装的组件执行反应状态更新”警告Javascript 反应本机清除间隔不停止间隔,javascript,reactjs,react-native,Javascript,Reactjs,React Native,当我离开组件时,ticker从不停止并抛出“无法对未安装的组件执行反应状态更新”警告 我将clearInterval更改为console.log,并且在更改组件时正确打印,但clearInterval没有清除间隔。我完全不知道为什么会发生这种情况,但在查看文档3个小时后,我发现: useEffect(() => { // Start elapsed ticker const levelTicker = setInterval(() => { console
我将clearInterval更改为console.log,并且在更改组件时正确打印,但clearInterval没有清除间隔。我完全不知道为什么会发生这种情况,但在查看文档3个小时后,我发现:
useEffect(() => {
// Start elapsed ticker
const levelTicker = setInterval(() => {
console.log("TICK...");
if (getCurrentTournament() != null) {
setFormattedLevelTimer(
moment
.utc(
new Date(getCurrentTournament().levelEnds).getTime() - getTime()
)
.format("mm:ss")
);
}
}, 1000);
return () => {
clearInterval(levelTicker);
};
}, []);
我从clearInterval()
切换到clearTimeout()
,它似乎成功地停止了间隔
it's worth noting that the pool of ID's used by SetInterval() and setTimeout() are shared, which means you can technically use clearInterval() and clearTimeout() interchangeably.