我尝试这样做:
const rowObj = {key: value};
const rowIndex = 2;
this.setState({
data: update(this.state.data,
{ [rowIndex] : { $push : [rowObj] } }
)
})
但是,它会引发如下错误:
Uncaught Error: update(): expected target of $push to be an array; got undefined.
答案 0 :(得分:1)
由于您要推送到数组即数据,因此您不想指定索引并像这样写
ProgressBar
否则,如果要为数组中的特定索引设置值,则应使用const rowObj = {key: value};
this.setState({
data: update(this.state.data,
{ $push : [rowObj] }
)
})
$set
答案 1 :(得分:1)
尝试这样
const rowArray = [{key: value},{key: value},{key: value}];
const obj = {key: value}
const rowIndex = 2;
rowArray.splice(rowIndex, 0, obj);
this.setState({ data: update(this.state.data,{rowArray : {$set : rowArray}} ) });