我只是使用immutability helper
在特定索引处更新数组中的对象。
import update from 'immutability-helper';
const { updateData, surveysData } = this.state;
surveysData.map((item, i) => {
if (item.id === updateData.id) {
const updatedData = update(surveysData, { $splice: [[i, 1, updateData]] });
this.setState({ surveysData: updatedData });
}
});
此处updateData是{object}。和 surveyData是[array]
我也尝试使用$set
,但仍然遇到相同的错误
surveysData.map((item, i) => {
if (item.id === updateData.id) {
this.setState({ surveysData: update(surveysData, { [i]: { $set: updateData } }) });
}
});