0

First of all, it must be said that this may be a setState issue as I have been having some trouble with that but I cannot work out why this while loop wont work.

code:

dealerTwisted = () => {
    console.log(this.state.dealersOverallTotal, 'total on entry');
    let looping = true;
    while(looping){
      console.log(this.state.dealersOverallTotal,'same');
      if(this.state.dealersOverallTotal < 17){
        this.deal2Dealer();
        let dealersDeck = this.state.dealersDeck;
        let newDealersDeckTotal = [];
        for (var i=0; i < dealersDeck.length; i++){
          newDealersDeckTotal.push(dealersDeck[i].rankValue)
        }
        let total = newDealersDeckTotal.reduce(function(a, b) {
          return a + b;
        },
        0);
        console.log(total, 'tot');
        this.setState({ dealersOverallTotal: total });
      }
      else{
        console.log(this.state.dealersOverallTotal, 'logging as greater than 17');
        if(this.state.playersOverallTotal > this.state.dealersOverallTotal){
          console.log('player wins!');
          looping = false;
        }
        else if (this.state.playersOverallTotal > this.state.dealersOverallTotal){
          console.log('its a tie!');
          looping = false;
        }
        else {
          console.log('dealer wins!');
          looping = false;
        }
      }
    }
  };

The way I think it is and the way I want it to work is as follows: the function gets called, the loop is entered, you enter the if block if this.state.dealersOverallTotal is less than 17. that total then gets updated and then you begin the loop again. you continue to enter the if block until you get to a number higher than 17. once you do you enter the else block and then whichever condition you hit in there, you break out.

interesting I have just debugged and found that this line (the fist console log) console.log(this.state.dealersOverallTotal, 'total on entry'); gives me the same number every time, i.e. not updating once inside. can anyone see why?

The worm
  • 5,580
  • 14
  • 36
  • 49
  • Do you have an `i` symbol next to your debugged log? Sometimes it shows you the calculated (finished) value rather than the value at the time of the log. – Summer Developer Jan 03 '17 at 16:53
  • ` this.setState({ dealersOverallTotal: total });` this function is probably not doing what it is supposed to do. Can you add it to the question – Glubus Jan 03 '17 at 16:56
  • Just use 'this.state.dealersOverallTotal = total'? – allnodcoms Jan 03 '17 at 17:01
  • @allnodcoms I have been informed that is unsafe.. – The worm Jan 04 '17 at 09:27
  • @Theworm - Have just had a read, and it's only unsafe if you use both. If you ignore it completely and only use assignments it will solve your problem. 'setState' doesn't immediately alter the value. According to the React docs, it 'sets up a pending state transition...' which can be delayed or batched for performance reasons. – allnodcoms Jan 04 '17 at 10:47
  • @allnodcoms I am using setState a lot though so really need to use it and work out how to get around the async problem – The worm Jan 04 '17 at 10:58
  • @Theworm - I'd still try it here as a use case. If it resolves this issue you at least know that you've identified the problem. – allnodcoms Jan 04 '17 at 11:08
  • @Theworm - You are essentially asynchronously altering a value in a synchronous loop, so there are three options: 1) Make you loop sync with React, maybe using a callback? I don't know enough about React to advise on this. 2) Use immediate assignments throughout. 3) Use assignments here and hope React updates its cached values "when the dealing's done..." (to misquote Kenny Rogers!) – allnodcoms Jan 04 '17 at 11:12

1 Answers1

0

React's setState is asynchronous, so your this.state.dealersOverallTotal never reaches 17.

setState in reactjs is Async or Sync

Community
  • 1
  • 1
Assan
  • 428
  • 1
  • 4
  • 10