【发布时间】:2018-01-25 17:11:56
【问题描述】:
我想使用解构解决方案在我的 React 容器中重新排序我的数组,这应该非常简单。
给定一个数组a1 = ['hello', 'hi', 'hola']
componentDidMount() {
const { a1 } = this.props
this.a2 = []
[a2[2], a2[0], a2[1]] = a1 --> this line give an error!!
console.log('new ordered array', a2) // ['hola', 'hello', 'hi'] --> this print properly my new array
}
如果我尝试控制台日志或在渲染中使用它,我得到undefined
我在该行得到的错误是:
Uncaught (in promise) TypeError: Cannot set property '#<Object>' of undefined
我真的不明白为什么我可以在console.log 中正确打印该值,但是当我尝试在代码中实际使用它时它不起作用。
这可能与 React 循环有关?
我也尝试在我的州内使用它,但遇到了同样的错误。
【问题讨论】:
-
看看有错误的那一行,你有一个点(
.)应该是逗号 -
这叫解构,不是解构。
-
感谢@PeterMader
标签: javascript arrays reactjs ecmascript-6