I want to push (splice) object from variable into array, but it causes an infinite loop, the point is to replace object in array with another object from React state.
handleDuplicate = (id, nextId) => {
let items = [...this.state.items];
let currentItem = this.state.items[id];
if (items && currentItem) {
console.log(currentItem); // --- logs object from react state properly
for (var i = 0; i < items.length; i++) {
if (items[i].id == nextId) {
items.splice(i, 1);
// items.splice(i, 0, currentItem); // --- causes infinte loop
items.splice(i, 0, { id: 111, test: "test" }); // --- runs ok
i--;
console.log(items);
} else {
console.log("---");
}
}
}
this.setState({ items });
};