프로젝트를 진행하면서 기존에는 내장 API 보다는 axios 사용해 개발을 했었다.
여러가지 이유가 있지만 interceptors등 Fetch API에 없는 기능을 axios에서 제공 해서 사용했다.
해당 게시글에서는 Fetch API
와 axios
의 기능을 비교해 보려고 한다.
const fetchData = async () => {
const response = await fetch('/api/data');
if (!response.ok) {
throw new Error('Network response was not ok');
}
return await response.json();
};
const MyComponent = () => {
useEffect(() => {
fetchData()
.then(data => console.log(data))
.catch(error => console.error('Fetch error:', error));
}, []);
return <div>Check console for data</div>;
};
export default MyComponent;