简体   繁体   English

应用程序中的计数器未按预期 function

[英]Counter in the app doesn't function as intended

 if(this.state.current=== this.state.dataSet.length-1){
    if(this.state.choice.correct===2) { 
      this.setState({IsPassed:true})
    }
    else if(this.state.choice.incorrect===2){ 
      this.setState({IsFailed:true})
  }}else{
    this.setState({current:this.state.current+1})
} 

 import React from 'react' function IsPassed() { return ( <div> <h2 className="IsPassed">You are Passed;</h2> </div> ) } export default IsPassed;

 import React from 'react' function IsFailed() { return ( <div> <h2 className="IsFailed">You are Failed</h2> </div> ) } export default IsFailed;

 handleClick(choice){ if(choice===this.state.dataSet[this.state.current].correct){ this.setState({correct:this.state.correct+1}) } else{ this.setState({incorrect:this.state.incorrect+1}) } /*this.setState({isFinished:true})*/ if(this.state.current=== this.state.dataSet.length-1){ if(this.state.correct===2) { this.setState({IsPassed:true}) } else if(this.state.incorrect===2){ this.setState({IsFailed:true}) } } else{ this.setState({current:this.state.current+1}) } }

total 3 questions but it displaying 4 entries containing correct and incorrect Whenever i am selecting the correct option first and then incorrect,so when the third time when i select the correct option, it counts initially from 0 and displays the message of you are passed总共 3 个问题,但它显示 4 个包含正确和不正确的条目每当我先选择正确选项然后不正确,所以当我第三次选择 select 正确选项时,它最初从 0 开始计数并显示您通过的消息

You can call this inside componentDidUpdate() function to see if the test is finished and the user has passed or failed:您可以在 componentDidUpdate() function 中调用它来查看测试是否完成以及用户是否通过或失败:

if(this.state.current=== this.state.dataSet.length-1){
        if(this.state.correct===2) { 
          this.setState({IsPassed:true})
        }
        else if(this.state.incorrect===2){ 
          this.setState({IsFailed:true})
        }
    }
}

And in the handleClick:在handleClick中:

    if(choice===this.state.dataSet[this.state.current].correct){
          this.setState({
           correct:this.state.correct+1,
           current: this.state.current+1
          })
        }
        else{
          this.setState({
           incorrect:this.state.incorrect+1, 
           current:this.state.current+1
           })
        }

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

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