data.js
public
폴더 > data
폴더 > data.json
const [ commentList, setCommentList ] = useState([]);
useEffect(() => {
fetch('http://localhost:3000/data/commentData.json', {
method: 'GET'
})
.then(res => res.json())
.then(res => {
setCommentList(res.data);
});
}, []);
<ul>
{commentList.map(comment => {
return (
<Comment
clickEvent={changeColor}
comment={comment}
/>
);
})}
</ul>
fetch()
함수를 이용한 API 통신handleLogin = () => {
const { userID, userPW } = this.state;
fetch("http://3.34.133.239:8000/account/signin", {
method: "POST",
body: JSON.stringify({
email: userID,
password: userPW,
}),
})
.then((response) => response.json())
.then((result) => {
if (result.token) {
localStorage.setItem("token", result.token);
history.push("main");
} else if (result.message === "UNAUTHORIZED") {
alert("아이디나 비밀번호를 확인해주세요.");
}
});
}
};
(참고) [React] Refactoring Check List
console.log
, 불필요한 주석 삭제className
)reset.scss
& common.scss
- 초기 세팅의 중요성className
)className
의 동적 사용 - inline style 지양destructuring
)Array.map()
활용계산된 속성명
<a>
vs <Link>
import
순서 (React > Pacakges, Library > Component > Style)