Why can't I push values in my state array in react.js?

前端 未结 5 994
后悔当初
后悔当初 2021-01-15 09:40

I can add 1 item to the array it logs [\"50\"] in the console. But when I try to add a second value I get this error \"currentScores.push is not a f

5条回答
  •  情书的邮戳
    2021-01-15 10:32

    Push returns the new length property of the object upon which the method was called.

    You set the state of scores to the new length each time.

    addScore() {
            console.log(this.state.scores);
            this.setState(state=> ({
                scores: [...state.scores, state.scoreInput]
                })
             );
          }
    

提交回复
热议问题