简体   繁体   English

navigation.push 上的 clearInterval() 反应

[英]clearInterval() on navigation.push react

I wrote some code to set an Interval on my dealScreen :我写了一些代码来在我的dealScreen上设置一个 Interval :

let timer;
  useEffect(() => {
        timer = setInterval(() => { 
          console.log('test');
        }, 1000);
    return () => clearInterval(timer);
  }, []);

When navigating to another page with navigation.push the Interval isn't cleared.使用navigation.push导航到另一个页面时,不会清除 Interval。 How can I force the interval to be cleared on navigation .如何强制在navigation时清除间隔。 Because it is a stack so I think the page is still behind the new page.因为它是一个堆栈所以我认为该页面仍然在新页面的后面。

When using navigation.replace it works but then I have no correct history back.使用navigation.replace时它可以工作,但是我没有正确的历史记录。

Try to call the clearInterval(timer) function when you trying to navigate.尝试导航时尝试调用clearInterval(timer)函数。

Example:例子:

  let timer;
  useEffect(() => {
    timer = setInterval(() => {
      console.log('test');
    }, 1000);
  }, []);

  const navigateToNextScreen = () => {
    clearInterval(timer);
    navigation.push('Next Sreen');
  };

I hope this help you out.我希望这可以帮助你。 Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM