Patrik Rikama-Hinnenberg
Patrik Rikama-Hinnenberg

Reputation: 1610

Remove object by id from array in React hooks state

How would I remove a specific id in this setup. I tried to use filter: setNotesDummyData(notesDummyData.filter((o) => { myCondition })); inside the onChangeItemName function but can't return right conditions so only one item will get removed.

const NotesContainer = ({

}) => {
  const [notesDummyData, setNotesDummyData] = useState([
    {
      id: '5',
      title: 'Holland',
    },
    {
      id: '7',
      title: 'Russia',
    },
  ]);

  const onChangeItemName = (itemId) => {

    //Remove item with specific itemId

  };

Upvotes: 2

Views: 5487

Answers (1)

AvcS
AvcS

Reputation: 2323

const onChangeItemName = (itemId) => {
    setNotesDummyData(notesDummyData.filter(({ id }) => id !== itemId));
};

Upvotes: 3

Related Questions