xxxxxxxxxx
// method .delete
axios.delete("URL")
.then(res => console.log(res.data));
xxxxxxxxxx
//instead of
axios.put(url, { foo: "bar" });
//wrap the data in the data propery of a new object
axios.delete(url, { data: { foo: "bar" } });
xxxxxxxxxx
axios.delete(url, { data: { foo: "bar" }, headers: { "Authorization": "***" } });
xxxxxxxxxx
import axios from "axios";
/// DELETE REQUEST
handleDeleteById(id).then((res) => {
alert("deleted")
});
function handleDeleteById(id) {
// console.log(data);
// {name: "mysore", population: 11111, country: "India"}
return axios({
baseURL: "baseURL",
url: `/${id}`,
method: "DELETE"
});
}
xxxxxxxxxx
//Delete contacts
const deleteContctOf = (person) => {
console.log(person)
console.log('delete contact ' + person.id + ' ????')
if (window.confirm("Do you really want to delete this person")) {
contactService
.remove(person.id)
.setPersons(persons.map(person => person.id !=== id))
this solved the question
} else {
return
}
}
xxxxxxxxxx
import axios from "axios";
import React from "react";
const baseURL = "https://jsonplaceholder.typicode.com/posts";
export default function App() {
const [post, setPost] = React.useState(null);
React.useEffect(() => {
axios.get(`${baseURL}/1`).then((response) => {
setPost(response.data);
});
}, []);
function deletePost() {
axios
.delete(`${baseURL}/1`)
.then(() => {
alert("Post deleted!");
setPost(null)
});
}
if (!post) return "No post!"
return (
<div>
<h1>{post.title}</h1>
<p>{post.body}</p>
<button onClick={deletePost}>Delete Post</button>
</div>
);
}