Skip to content
Advertisement

How to remove Item from an array with react and firebase

When I add an item to the array it works but the splice does not.

const handleSportsFollowed = async (sport) => {
  if (selectedSports.includes(sport)) {
    selectedSports.splice(sport, 1);
    alert("Removed");
  } else {
    selectedSports.push(sport);
  }
}

Advertisement

Answer

You can remove elements that are equal to sport from the array using:

selectedSports = selectedSports.filter(x => x != sport)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement