为什么我的ForEach执行没有被调用? ReactJs?

时间:2018-06-07 23:59:22

标签: javascript reactjs

我在函数内部创建一个ForEach,即使没有调用它也会执行,因此它会渲染屏幕。

我要做的是:当我点击startGame函数时,位置[1]中数组的元素是5,但是一旦它呈现屏幕它就已经开始了6。

我点击时想要的finishGame函数我希望它显示为6,但在我点击该函数之前它已经变为6。

startGame:



startGame = () => {
  this.state.cardsDealer.forEach(function(i, index, array) {
    if (index === 1) {
      array[1] = '6'
    }
  })
}






finishGame = () => {
    this.state.cardsDealer.forEach(function(i, index, array) {
      if (index === 1) {
        array[1] = '5'
      }
    })




有谁知道为什么?或者,如果我做的方式错了,我想要的是什么?

1 个答案:

答案 0 :(得分:-1)

startGame() {
  this.state.cardsDealer.forEach(function(i, index, array) {
    if (index === 1) {
      array[1] = '6'
    }
  })
}

试试吧