0

I'm implementing a To-Do list in React Native, but for some reason after I remove a task, the next task in the array takes some values of the one I removed. I made a copy of the state array and made changes to it, console.log them to see that everything was right, then setState with that copy. I think it must be an issue with the sate. Here is simplified version of my remove_task function. Am I doing something wrong?

 _removeTask = (tindex) => {
/*removes a task in the tasks array*/
//make copy of state array
var tasks = [...this.state.Tasks];

var taskCount = 0;

tasks.splice(tIndex, 1);

//update the index of every quest
tasks.forEach(
  (value, index) => {
    value.tIndex = index;
    value.selected = false;
    value.isActiveDummyTask = false;
    value.isInEditMode = false;
    taskCount++;
  }
)
this.setState({ 
  Tasks: tasks,
  TCount: taskCount,
});

}

  • Does this answer your question? [How do I remove a particular element from an array in JavaScript?](https://stackoverflow.com/questions/5767325/how-do-i-remove-a-particular-element-from-an-array-in-javascript) – Hardik Virani Jan 09 '20 at 04:22
  • No, because the array is being edited correctly on js. But for some reason it is working with the React-Native state. – Telegonicaxx Jan 09 '20 at 04:31
  • Are you setting the props key upon mapping the element ? Key has to be unique. – David Desjardins Jan 09 '20 at 04:33
  • you are facing issue that where you have used this state in UI, it is not updating after setState right? I mean deleting works logically but not actully removing from UI? – Jaydeep Galani Jan 09 '20 at 04:34
  • ohhhhhhh, maybe that's i the reason. I'm setting them as the index: _renderTask = (value, index) => { //render and pass function props into the Task component return } – Telegonicaxx Jan 09 '20 at 04:36
  • Changed the Key to match the tindex value, but still no avail... – Telegonicaxx Jan 09 '20 at 04:42
  • 1
    Is it not a good practice but try setting the key to JSON.stringify(task). If it fixes your problem you know it is the key. – David Desjardins Jan 09 '20 at 05:21
  • 1
    David, I love you. Thank you so much! It actually fixed the problem. I now know it was a problem with the key! Again Thanks you some much! You are a hero. No, you are a legend! No, you are the best. – Telegonicaxx Jan 09 '20 at 06:15

0 Answers0