Deleting a Todo

Deleting a Todo is going to be very similar to creating a Todo, so there’s not actually that much that we have to describe here. The biggest and most important things are that the method is set to DELETE and the id of the Todo we want to delete is passed in via the URL:

  async removeTodo(removeItemId) {
const res = await fetch(`/api/todos/${removeItemId}`, {
method: 'DELETE',
headers: { accept: 'application/json', 'content-type': 'application/json' }
});
if (res.status === 200) {
const filteredItems = this.state.items.filter(todo => {
return todo.id !== removeItemId;
});
this.setState({ items: filteredItems });
}
}

We'll also need to change how the removeTodo function is called from src/Todo/Todo.js, so open that file up and change the argument that it passes in to be by the Todo's id instead of by its description! As follows:

  removeTodo() {
this.props.removeTodo(this.props.id);
}

This should get us most of the functionality that we want, but the problem we have right now is that our code is not nearly as simple to test as we'd like. In fact, we actually have a few failing tests that we'll need to fix up!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.61.246