问题
您正在添加多个具有相同 rowId 的自定义行,因此当您过滤它们时,您删除的内容超出了您的预期。
您还错误地将newRow.rowId 与this.state.customRow.rowId 进行了比较,因为它是一个数组,因此当然是未定义的,并更新您的状态以将customRow 嵌套到另一个数组中。
handlingDeleteRow = (index, newRow) => {
let newdel = this.state.customRow.filter(
(newRow) => newRow.rowId !== this.state.customRow.rowId // <-- incorrect comparison
);
this.setState({
customRow: [newdel] // <-- nest array in array
});
console.log(this.state.customRow);
};
解决方案
我建议为您添加的自定义行添加一个 guid 并在其上进行匹配。
import { v4 as uuidV4 } from 'uuid';
...
addCustomFile = (index) => {
this.setState({
resError: null,
customRow: [
...this.state.customRow,
{ rowId: index, fileData: false, fileName: false, guid: uuidV4() }
]
});
};
删除时使用新的guid 属性。
handlingDeleteRow = (index, newRow) => {
let newdel = this.state.customRow.filter(
(row) => row.guid !== newRow.guid
);
this.setState({
customRow: newdel
});
};
演示