promise 기반의 HTTP 클라이언트 라이브러리, 브라우저 및 Node.js 환경에서 모두 사용 가능하며 브라우저 환경에서는 XMLHttpRequests를 생성하고 Node.js에서는 http 요청을 생성.
Axios는 백엔드와 통신하기 위해 AJAX 요청을 만들고 응답을 다루는데 사용되며 다른 비동기 통신 api보다 사용하기 쉽다.
크게 get,post,put,patch, delete가 존재 하며 비동기 처리 방식으로 이루어져 async함수 및 await 로 주로 구성을 한다
useEffect(() => {
// Fetch data from the API
const fetchData = async () => {
try {
const response = await axios.get(
"http://localhost:8080/api/secretboard/read"
); // Replace with your API endpoint
setData(response.data);
} catch (error) {
console.error("Error fetching data", error);
}
};
fetchData();
}, []);

const handleAddPost = async (newPost) => {
try {
const response = await axios.post(
"http://localhost:8080/api/secretboard/save",
newPost
); // newPost라는 새로운 값을 가진 객체나 변수를 더한다
setData([...data, response.data]); // 새로운 값을 기존에 데이터 값에 더한다
} catch (error) {
console.error("Error adding post:", error);
}
};

const handleDeletePost = async () => {
try {
await axios.delete(`http://localhost:8080/api/secretboard/delete/${id}`);
//특정 id에 해당하는 값을 삭제하는
} catch (error) {
console.error("Error deleting post", error);
}
};
const handleUpdatePost = async () => {
try {
await axios.put(`http://localhost:8080/api/secretboard/update/${id}`, post);
// 특정 id에 해당하는 값을 post로 아예 덮어버린다.
const response = await axios.get(`http://localhost:8080/api/secretboard/read/${id}`);
// 업데이트 후에 새로 get으로 불러서 post객체에 넣어야 업데이트 되는 형태가 바로 보였음
setpost(response.data);
} catch (error) {
console.error("Error updating post", error);
}
};

참고: https://sokkung.tistory.com/213
참고: https://velog.io/@hyeseung/axios-%EC%9A%94%EC%B2%ADGETPOSTPUTPATCH
참고: https://velog.io/@hyunn/Axios-%EB%9D%BC%EC%9D%B4%EB%B8%8C%EB%9F%AC%EB%A6%AC-%EA%B0%9C%EB%85%90-%EC%A0%95%EB%A6%AC