score:1

Accepted answer

yes, splice affects the array it acts on so don't use in this way. instead you need to create a new array of the correct elements:

this.setstate({
    listarray: this.state.listarray.filter((el, idx) => idx !== i);
});

if you want to remove only the first instance, maybe couple with a findindex (although indexof would work in your example as well) first:

delete(elem) {
    const idxtofilter = this.state.listarray.findindex(el => el === elem);

    if (idxtofilter < 0) {
        return;
    }

    this.setstate({
        listarray: this.state.listarray.filter((el, idx) => idx !== idxtofilter);
    });

}

this creates a new array without modifying the old which will cause anything that reacts to listarray changing to be notified since the reference has changed.


Related Query

More Query from same tag