← Back to all tutorials
React TutorialEpisode 31

Deleting Blogs

Implement delete functionality to remove blog posts from the server.

Add a delete button to remove blogs from the database.

Delete Request

const BlogDetails = () => {
  const { id } = useParams();
  const history = useHistory();

  const handleDelete = () => {
    fetch("http://localhost:8000/blogs/" + id, {
      method: "DELETE"
    }).then(() => {
      history.push("/");
    });
  };

  return (
    <div>
      {blog && (
        <article>
          <h2>{blog.title}</h2>
          <p>{blog.body}</p>
          <button onClick={handleDelete}>Delete</button>
        </article>
      )}
    </div>
  );
};