【发布时间】:2017-01-29 22:10:34
【问题描述】:
我想用 immutability-helper 函数更新我的状态 update (https://github.com/kolodny/immutability-helper)
state = {
field: '',
items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6']
};
addElementToList() {
const i = this.state.items.length;
const newState = ()=> {
update(this.state,
{
items: {
$push: [
{
[i]: this.state.field
}
]
}
}
)
};
this.setState = newState;
}
很遗憾,函数执行时出现以下错误。
Uncaught TypeError: (0 , _immutabilityHelper.update) is not a function
我错过了什么,对此有什么想法吗?
【问题讨论】:
-
仅使用 JavaScript 的
Array.prototype.concat(或扩展语法)和Array.prototype.slice在不发生突变的情况下从数组中添加/删除元素不是更容易吗?
标签: javascript reactjs immutability