Axios에 대해서 알아보자.
가장 많이 사용되고 있는 자바스크립트 HTTP 클라이언트 라이브러리
HTTP 요청을 Promise 기반으로 처리함
// get 함수는 파라미터로 전달된 주소에 GET 요청을 해 줌
// 결과는 .then을 통해 비동기적으로 확인
axios.get('https://jsonplaceholder.typicode.com/todos/1')
.then(response => {
setData(response.data);
});
// async/await 방식
// async () => { } 와 같이 적용됨
async () => {
try {
const response = await axios.get(
'https://jsonplaceholder.typicode.com/todos/1'
);
setData(response.data);
} catch (e) {
console.log(e);
}
}