score:14

Accepted answer

two issues there:

  1. you're seeming to try to direct modify this.state.tasks. it's important not to do that, never directly modify this.state or any object on it. see "do not modify state directly" in the react documentation for state.

  2. you're passing an object to setstate that is derived from the current state. it's important never to do that, too. :-) instead, pass setstate a function and use the state object it passes you when calling that function. from "state updates may be asynchronous" in the documentation:

    because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state... [instead]...use a second form of setstate() that accepts a function rather than an object.

    (my emphasis)

i figure your remove on an array was intended to be hypothetical, but for the avoidance of doubt, arrays don't have a remove method. in this case, the best thing to do, since we need a new array, is to use filter to remove all entries that shouldn't still be there.

so:

deletetask(tasktodelete) {
    this.setstate(prevstate => {
        const tasks = prevstate.tasks.filter(task => task.name !== tasktodelete);
        return { tasks };
    });
}

score:0

i have followed below steps to delete a particular selected object from the state array:

here i am using a list of checkboxes, when i am selecting a checkbox it will add it in the state array and when it gets de-selected then it will get deleted from the array.

if (checked) {
            var tempobject = { checkboxvalue: data, label: title }
            this.state.checkboxstate.push(restemp);
        } else {
            var element = data; //data is coming from different method.
            for (let index = 0; index < this.state.checkboxstate.length; index++) {
                if (element === this.state.checkboxstate[index].checkboxvalue) {
                    this.state.checkboxstate.splice(index, 1);
                }
            }
        }

i got stuck for this question and i am sharing my solution. hope it will help you.

score:2

you can use higher order function array#filter to delete the task.

let updatedtasks  =     this.state.tasks.filter(task => task.name !== tasktodelete);

this.setstate({ tasks: updatedtasks });

score:3

you can use filter to remove one object from an array following the immutable pattern (filter will create a new array) :

deletetask(tasktodelete) {
    const newtaskarray = this.state.tasks.filter(task => task.name !== tasktodelete);
    this.setstate({ tasks: newtaskarray });

}

edit : codepend of the solution : https://codepen.io/dyo/pen/zvpoyp

score:3

you can implement deletetask method as below:

deletetask(tasktodelete) {
  this.setstate((prevstate, props) => {
    const tasks = [...prevstate.tasks];
    const indexoftasktodelete = tasks.findindex(
      task => task.name === tasktodelete
    );
    tasks.splice(indexoftasktodelete, 1);
    return { tasks };
  });
}

a. find the index of tasktodelete.

b. then use splice method to delete the item from the collection

c. then call setstate to update the state with tasks.

score:6

you could simply filter the array :

this.setstate(prevstate => ({
    tasks: prevstate.tasks.filter(task => task.name !== 'tasktodelete')
}));

also when updating based on this.state, its better to use the function form because setstate is async.


Related Query

More Query from same tag