Instead of getting current state I get prevState in React app

  reactjs, setstate

I have hard time creating puzzle react app. When user finnish the puzzle i want to and the game and set some states, like alert of winning and start game over. But I do not know when to implement it. I tried on drag end, when user puts last puzzle it receiving not the last state but the previous one and when user switches one more time to ruin the puzzle it receives the state I need. Also tried to implement it onMouseUp but same effect. I searched for that but only solutions I found was setTimeout which doesn’t help and componentDidUpdate but in my case it triggers infinitife loop. Anyway, what I am trying is:
I check if arr of puzzles is empty, then I check if every puzzle is on its place ==>

 if (this.state.arr.length === 0) {
   if (this.state.puzzle["one"].id === 1 && this.state.puzzle["two"].id === 2 ...) {
     this.setState({stop: true, win: "You win!"})
   }
}

If anyone could get me some tips, how to solve it I would really appreciate it.

Source: Ask Javascript Questions

LEAVE A COMMENT