score:3

Accepted answer

indexof will just look for the object with the exact same object reference, so even if you create a new object with the exact same data as the todo you want to remove it will not work, sadly.

you could instead filter out the item with the same key as snapshot.key.

this.todosref.on('child_removed', snapshot => {
  this.setstate(previousstate => {
    const todos = previousstate.todos.filter(todo => todo.key !== snapshot.key);

    return { todos };
  });
});

score:2

splice returns the deleted elements if any. since in your example you are mutating the array, the new state will be set to the array containing the deleted element.

this.state.todos.splice(index, removedtodo);

is supposed to be

this.state.todos.splice(index, 1);

and this should be performed before setting the state.

this.todosref.on('child_removed', snapshot => {
  const removedtodo = {
    key: snapshot.key,
    value: snapshot.val()
  }
  const index = this.state.todos.indexof(removedtodo);

  let todos = this.state.todos.splice(index, removedtodo);
  todos.splice(index, 1);

  this.setstate({
    todos: todos
  });
});

Related Query

More Query from same tag