23.1.21 기준
import axios from 'axios'
// axios 설치 및 import
const Blog = () => {
interface A {
title: string;
content: string;
id:string;
}
// type 지정 => 타입에러 나는 부분 수정하여 타입 지정
...
...
const fetchContents = async () => {
const {data} = await axios.get('http://localhost:3001/contentList')
// data fetcing
setContentList(data)
}
useEffect(() => {
fetchContents();
},[])
// 랜더링 위한 useEffect
const addContent = () => {
if(content.title === "" || content.content === "") {
return alert("빈값은 등록할 수 없습니다!")
}
setContentList([...contentList, {...content, id:uuid()}])
axios.post('http://localhost:3001/contentList', content)
// post 위한 설정
setContent(contentInitialState)
}
오늘 작업의 교훈 : axios의 get, post가 핵심이었다.