-
[React] fetch와 axios개발공부/React 2024. 3. 25. 19:23
본 글은 리액트에서 네트워크 통신을 가능하게 해주는 fetch와 axios에 대해 다룬 내용입니다
목차
fetch
- JS 내장 라이브러리
- Promise 기반의 인터페이스를 사용해 비동기적으로 요청을 처리
try { let response = await fetch(url); if (!response.ok) { throw new Error('Network response was not ok'); } let data = await response.json(); console.log(data); } catch (error) { console.error('Fetch error:', error); }
axios
- Promise 기반의 API를 사용해 비동기 요청 처리
- HTTP 요청과 응답을 쉽게 조작할 수 있는 다양한 기능 제공
- 인터셉터(interceptors)를 사용해 요청과 응답을 전처리하거나 후처리 가능
import axios from 'axios'; try { let response = await axios.get(url); console.log(response.data); } catch (error) { console.error('Axios error:', error); }
728x90'개발공부 > React' 카테고리의 다른 글
[React] useState, useReducer hook (0) 2024.03.02 실시간 채팅 구현 (0) 2024.01.24 상태 관리 (0) 2024.01.16 DOM에 직접 접근하기 (useRef) (0) 2023.10.26 리액트 프로젝트 구조 (0) 2023.10.25