0

In react native, I'm using those functions to save and delete items from an array but it only delete last item, what I'm supposed to change ?

SaveValue = () => {
  const newFavs = [...this.state.favs, this.state.UserInput];
  this.setState({ favs: newFavs, UserInput: '' }, () => {
      AsyncStorage.setItem("Favorites", JSON.stringify(this.state.favs));
      Keyboard.dismiss()
  });
};

FetchValue = () => {
  AsyncStorage.getItem("Favorites").then((value) => {
    this.setState({
      favs: JSON.parse(value)
    });
  }).done();
};


RemoveValue(item){
    const index = this.state.favs.indexOf(item);
    const newArray = [...this.state.favs];
    newArray.splice(index,1);
    this.setState({ favs: newArray });
    AsyncStorage.setItem("Favorites", JSON.stringify(newArray));
}
blacksun
  • 295
  • 2
  • 3
  • 14
  • Duplicate: https://stackoverflow.com/questions/5767325/how-do-i-remove-a-particular-element-from-an-array-in-javascript - but why not in place: `this.state.favs.splice(index,1);` ? – mplungjan Feb 06 '18 at 06:33
  • 3
    Possible duplicate of [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) – Vikas Yadav Feb 06 '18 at 06:38

0 Answers0