如何在没有setState()的情况下导航到另一个页面,反应中的Unmount组件错误

时间:2017-11-30 10:25:52

标签: reactjs components unmount

我正在通过编写像app这样的倒计时学习React,一切正常但我遇到了问题,每当我尝试导航到另一个页面而没有按下暂停时,我会得到错误'只能更新挂载或安装组件,这个通常意味着你在Unmount组件上调用了setState()。它是什么意思,我该如何解决?非常感谢你。

import React from 'react';
import SubjectForm from './SubjectForm';
import {connect} from 'react-redux';
import {editSubject, removeSubject} from '../actions/subjectAction';

class StartPage extends React.Component{
    constructor(props){
        super(props);
        this.state={count:this.props.subject.hour*60*60+this.props.subject.minute*60,
                    name:'run',
                    alert:''
                    };    
        this.timer = null;
        this.stateChange=this.state.count
        this.originTime = this.props.subject.hour*60*60+this.props.subject.minute*60;
    }

    setStateCountdown=(num)=>{
        this.setState({count:num})
    }

    setStateAlert=(text)=>{
        this.setState({alert:text})
    }

    setStateButtonChange=(text)=>{
        this.setState({name:text})
    }

    begin=()=>{
        clearInterval(this.timer)
        this.timer=setInterval(()=>{
            this.stateChange--
            this.setStateCountdown(this.stateChange)
            let timeLeft=this.stateChange

            if(this.stateChange<0){
                this.setStateAlert('done');
                clearInterval(this.timer);
            return;
        }
            let hourleft = Math.floor(timeLeft / (60 * 60));
            let minuteleft = Math.floor((timeLeft % (60 * 60)) / 60);
            let secondleft = timeLeft % 60;
            this.setStateAlert(`you have ${hourleft} hour ${minuteleft} minute and ${secondleft} second until reaching your goal`);


        },1000);
    }
    pause=()=>{
        clearInterval(this.timer)  
        console.log(this.state.count) 
        if(this.state.count<this.originTime){
            this.setStateButtonChange('resume')
        }
    }

    reset=()=>{
        const pressConfirm = confirm('are you sure you want to reset?')
        if(pressConfirm===true){
            console.log(this.state.count)
            clearInterval(this.timer)
            this.stateChange=this.originTime;
            this.callbackFuncCountdown(this.stateChange)
            this.callbackFunctionButtonChange('run')
            this.callbackFunctionAlert('lets begin your study again')
        }
    }

    render(){
        return(
            <div>
            <SubjectForm subject={this.props.subject}/>
            <button onClick = {this.begin}>{this.state.name}</button>
            <button onClick = {this.pause}>pause</button>
            <button onClick = {this.reset}>reset</button>
            <h3>{this.state.alert}</h3>
            </div>
        );
    }
};

const mapStateToProps = (state,props)=>{
    return{
        subject: state.subjects.find((subject)=>subject.id===props.match.params.id)
    };
}

export default connect(mapStateToProps)(StartPage);

我很抱歉长码。

1 个答案:

答案 0 :(得分:1)

导航到另一个页面卸载您的组件,但setInterval()中的功能仍在运行并尝试更新卸载的组件状态,您有2个选项,具体取决于您的应用所需的行为:

1 - 使用redux存储计时器并调度操作以启动/暂停/停止  它在任何地方(如果你导航,计时器将继续)

2 - 组件的componentWillUnmount()方法中的clearInterval(如果导航则重置或停止计时器)