Today Sangmin Learned
article thumbnail
[React] 서버와의 API 통신을 통해 받은 데이터 뿌리기
Web 2021. 8. 31. 15:07

이전에 Node.js + Express 환경에서 Mock data를 만들고 API를 설계하는 일까지 해봤다. 그래서 이번에는 그렇게 설계한 API를 클라이언트 단에서 API 통신을 통해 받아온 뒤 뿌려주는 것에 대해 포스팅해보려고 한다. 우선 async-await 함수에 사용할 fetcher 함수부터 만들어보자. 1. fetcher.js import axios from 'axios'; axios.defaults.baseURL = 'http://localhost:8000'; // 혹은 본인의 서버 URL const fetcher = async (method, url, ...rest) => { const res = await axios[method](url, ...rest); return res.data }..