【问题标题】:How to clearInterval on componentWillUnmount() correctly in react?如何在 react 中正确清除 componentWillUnmount() 上的 Interval?
【发布时间】:2019-08-28 16:27:50
【问题描述】:

我有一个表格可以呈现映射的patientInfo array。当patient.status 不为空/有值时,倒计时计时器开始为阵列中的个别患者倒计时。这发生在componentDidMount()。现在我想在componentWillUnMount() 上清除Interval,但倒计时并没有停止。

基本上我只需要在时间到 0 时清除倒计时。假设计时器从 60 秒到 0 时开始倒计时,清除间隔。没有停止按钮或类似的东西可以执行。当时间达到 0 秒时,我需要它自动清除间隔。当个体患者具有状态价值时,倒计时时间开始。希望这是有道理的

PatientInfo Array

patientInfo = [
           { count: 959999, room: "1", name: 'John Nero', status: ''},
           { count: 959999, room: "2", name: 'Shawn Michael', status: ''},
           { count: 959999, room: "3", name: 'Gereth Macneil', status: ''}
]

//starts countdown when the patient.status has value which comes from user input

componentDidMount() {

       this.countDownInterval = setInterval(() => {

           this.setState(prevState => ({

               patientInfo: prevState.patientInfo.map((patient) => {
                   if (patient.status !== '') {
                      // subtract a sec 
                       return { ...patient, count: patient.count - 1000};
                   }
                   return patient;
               })
           }));
       }, 1000);

   }


//when the patient.count is 950999 clearInterval doesn't work
//edited after some comments but still doesn't work

  componentWillUnmount() {

        this.state.patientInfo.map((patient) => {
            if (patient.count <= 950999) {
                clearInterval(this.countDownInterval);
            }
        });
    }
//after a few try the following seems to work but not sure if this is the correct way 


    componentDidMount() {


        this.countDownInterval = setInterval(() => {

            this.setState(prevState => ({

                patientInfo: prevState.patientInfo.map((patient) => {
                    if (patient.status !== '') {

                        if (patient.count <= 950999) {
                            clearInterval(this.countDownInterval);
                        }
                        return { ...patient, count: patient.count - 1000 };
                    }
                    return patient;
                })
            }));
        }, 1000);
    }

【问题讨论】:

  • [提示] 您可以简单地使用setInterval 而不是window.setInterval
  • 为什么要在 setState 中删除它。为什么不只是componentWillUnmount() { clearInterval(this.countDownInterval);
  • 您对clearInterval 的条件过于具体。为了让您清除间隔,您的组件必须安装 95 秒和 0.999 毫秒。我很确定 JS 甚至无法确保该级别的准确性。你的意思是patient.count &gt;== 950999
  • 我认为在componentWillUnmount 内部使用setState 的想法没有任何效果。
  • 嗨 Jereme,请阅读此内容 - stackoverflow.com/help/someone-answers,然后尝试关闭问题。

标签: javascript reactjs setinterval clearinterval


【解决方案1】:

我认为这里的问题是您正在清除从componentWillUnmount 调用 setState 的间隔。如果组件要卸载,则无需设置更多状态。该组件将不存在。

【讨论】:

    【解决方案2】:

    来自docs

    componentWillUnmount() 在组件被卸载和销毁之前立即调用。在此方法中执行任何必要的清理,例如使计时器无效、取消网络请求或清理在 componentDidMount() 中创建的任何订阅。

    不应该在 componentWillUnmount() 中调用 setState(),因为组件永远不会被重新渲染。一旦组件实例被卸载,它将永远不会被再次装载。

    你最多可以这样做,

    componentWillUnmount() {
       clearInterval(this.countDownInterval);
    }
    

    【讨论】:

    • 您好,我也尝试了您的建议,但似乎没有奏效。当患者的计数达到
    • 您在这里遇到了奇怪的用例。为什么需要这样做? componentWillUnmount 将在您的组件从 DOM 中删除之前被调用。以您的情况为例,您想在patient.count &lt;= 950999 的条件下@ 987654324@。但我认为这没有任何意义,因为每次组件挂载时,它都会再次以初始状态重新初始化。
    • 基本上我只需要在时间到达 0 分钟时清除倒计时。假设计时器从 60 秒开始倒计时到 0,清除间隔。没有停止按钮或类似的东西可以执行。我需要它在时间达到 0 秒时自动完成。当个体患者具有状态价值时,倒计时时间开始。 @ravibagul91 希望这是有道理的
    • 您只想清除间隔还是需要卸载组件?
    • 在这种情况下,您不需要componentWillUnmount,但请尝试仅在componentDidMount 中使用。
    猜你喜欢
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2019-07-25
    相关资源
    最近更新 更多