2017-11-30 19 views
0

私はアプリのようなカウントダウンを書くことで反応しますが、すべてうまくいきますが、押さずに別のページにナビゲートしようとすると問題が発生します私はエラーが発生する 'マウントまたはコンポーネントを更新することのみができますが、これは通常UnmountコンポーネントでsetState()を呼び出したことを意味します。それはどういう意味ですか、どうすれば修正できますか?事前にどうもありがとうございました。アンマウントコンポーネントの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:

答えて

1

コンポーネントをアンマウント別のページへの移動が、setInterval()内の関数がまだ実行してアンマウントコンポーネントの状態を更新しようとしている、あなたはあなたのアプリケーションのために必要な行動に応じて、2つのオプションを持っています一時停止/どこでも にそれを開始/停止するためにあなたのタイマーや派遣アクションを格納するためのReduxの(あなたがナビゲートした場合、タイマーは継続されます)

2 - あなたのコンポーネントのcomponentWillUnmount()方法でてclearInterval(あなたがナビゲートする場合は、タイマーをリセットまたは停止)

+0

ありがとう、私はしようとします両方、私は今後数日でそれをどうやって学ぶことができることを願って、少なくとも私は今何をすべきかの方向性を持っている – Nhat

関連する問題