ReactJS Array.push function not working in setState

前端 未结 5 1309
粉色の甜心
粉色の甜心 2021-01-17 22:18

I\'m making a primitive quiz app with 3 questions so far, all true or false. In my handleContinue method there is a call to push the users input from a radio fo

5条回答
  •  不思量自难忘°
    2021-01-17 22:53

    The recommended approach in later React versions is to use an updater function when modifying states to prevent race conditions:

    this.setState(prevState => ({
      userAnswers: [...prevState.userAnswers, this.state.value] 
    }));
    

提交回复
热议问题