【发布时间】:2020-11-08 18:37:44
【问题描述】:
下面的代码没有按照我的预期运行,console.log()(在//THIS处)怎么会受到代码的影响之后呢?如果我使用注释掉的代码运行以下代码,则顶部日志记录的结果会发生变化。反对在没有注释代码的情况下运行它,因此它不会改变。
关于下面代码的更多信息,next.shape[0] 返回一个看起来像这样的双精度数组:
[0,0,0,0],
[0,0,0,0],
[0,0,1,0],
[1,1,1,0]
state.array 也是一个大小约为 20x10 的双对象数组。 (你可能已经猜到了,我正在尝试构建俄罗斯方块)。我正在尝试将上述数组中的形状复制到 this.state.array 中,所以我创建了一个 tempField 数组,以便我可以对其进行编辑,然后将其粘贴到 setState() 中。
console.log(this.state.nextShape.shape[0])
var tempField = this.state.array.slice(0);
console.log("state array before: ")
console.log(this.state.array) // THIS
console.log("temp Field before: ")
console.log(tempField)// and THIS
// var tempPosition = this.state.currentPosition.slice(0);
// let z = 0;// for (let y = 0; y < this.state.nextShape.shape[0].length; y++) {
// for (let x = 0; x < this.state.nextShape.shape[0][y].length; x++) {
// if (this.state.nextShape.shape[0][y][x] == 1) {
// tempField[y][x + 3] = { occupied: false, color: this.state.nextShape.color };
// tempPosition[z] = [y, x + 3]
// z++;
// console.log(z)
// }
// }
// }
// console.log("temp Field after: ")
// console.log(tempField)
// this.setState({ array: tempField });
// this.setState({ currentPosition: tempPosition });
// this.setState((state, props) => ({ currentShape: state.nextShape },
// function () {
// this.setState({ nextShape: BlockTypes[Math.floor(Math.random() * BlockTypes.length)] });
// }));
简单来说,这样一段隐喻的代码是怎么来的:
var numberA = 0;
console.log(numberA);
numberA =+ 100;
console.log(numberA);
就我而言(第一段代码),两者都返回相同的答案?我该如何解决?
【问题讨论】:
-
请把你的组件代码完整。
标签: javascript arrays reactjs asynchronous state