简体   繁体   English

使用Break循环无限循环 ReactJs

[英]Loop for with loop infinite using Break? ReactJs

What happens: I have these two variables: ScorePlayer and ScoreDealer , which are the result of a sum of the elements contained in the main array of each of them cardsPlayer and cardsDealer . 发生了什么:我有两个变量: ScorePlayerScoreDealer ,它们是每个cardsPlayercardsDealer主数组中包含的元素cardsPlayer和的cardsDealer

And what I want is to do the verification. 我想要做的是验证。 If the value of the ScoreDealer variable is less than that of the ScorePlayer variable, I want it to add more elements in it ( ScoreDealer ) but I do not want the sum of the elements to exceed the value of 21 . 如果ScoreDealer变量的值小于ScorePlayer变量的值,我希望它在其中添加更多元素( ScoreDealer ),但我不希望元素的总和超过21的值。

I used the Break method but the infinite loop continues and hangs the application. 我使用了Break方法,但无限循环继续并挂起了应用程序。

The function that do this: 执行此功能:

finishGame = () => {
  const scorePlayer = this.state.cardsPlayer.reduce((a, b) => a + b);

  const scoreDealer = this.state.cardsDealer.reduce((a, b) => a + b);

  for (let i = scoreDealer; scoreDealer < scorePlayer; i++) {

    this.setState({
      cardsDealer: this.state.cardsDealer.concat(this.state.cards.splice(0, 1))
    })

    if (scoreDealer === 21) {
      break;
    }
  }
}

Can anybody help me? 有谁能够帮助我?

rossipedia state it perfect in this post. rossipedia在这篇文章中指出它很完美。

don't call setState in a loop. 不要在循环中调用setState。 What's happening here is exactly what the docs are referring to: this.state is returning the previous value, as the pending state update has not been applied yet. 这里发生的事情恰恰是文档所指的:由于尚未应用暂挂状态更新,因此this.state返回先前的值。

Calling setState in a loop only updates state 1 time 循环调用setState仅更新状态1次

The reason your app keeps on hanging is that this.state's values have not been updated yet and will not be updated till finishGame finishes executing, as well as some flawed logic as @Jayce444 stated in the comments above. 您的应用程序一直处于挂起状态的原因是this.state的值尚未更新,并且直到finishGame完成执行后才会更新,以及@ Jayce444在上面的注释中指出的某些错误逻辑。

A solution I would use while keeping most of your logic is this 我会在保留大部分逻辑的同时使用的解决方案是

finishGame = () => {
    // this is a copy of your array given I'm assuming its a 1D array and you can now mutate it
    const cardsDealer = [...this.state.cardsDealer];
    // if you chose to update scorePlayer you should declare it as let.
    const scorePlayer = this.state.cardsPlayer.reduce((a, b) => a + b);
    let scoreDealer = this.state.cardsDealer.reduce((a, b) => a + b);

    for (let i = 0; scoreDealer < 21; i++) {
        scoreDealer += cardsDealer.shift();

        // this will stop if card is >= to 21
        if (scoreDealer >= 21) {
            break;
        }
    }
    // this will set your cards to the new values after the loop is done running.
    this.setState({cardsDealer});
}

given there is a lot of fine-tuning needed in the above function I hope it gets you off to a good start. 鉴于上述功能需要进行很多微调,我希望它可以为您提供一个良好的开端。 Happy coding! 编码愉快!

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

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