Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 라우팅
- 브라우저 렌더링
- updater
- imagePreview
- 스파르타코딩클럽
- 채팅방
- 항해99
- Preview
- 비동기
- setstate
- socket.io
- useEffect
- react
- FileReader
- S3
- 접근 제한 라우팅
- CI/CD
- rendering
- qwe
- Redux
- routing
- 미리보기
- previousState
- 7기
- route
- 배포
- 후기
- Github Actions
- 개발
- 동기
Archives
- Today
- Total
삐옹
state 불변성을 지켜라(댓글 달기) 본문
const DetailComment = ({ boardId }) => {
const [newComment, setNewComment] = useState("");
const [commentArray, setCommentArray] = useState([]);
const updateComment = (e) => {
setNewComment(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
const currentKey = localStorage.getItem("jwt-token");
if (newComment === "") {
alert("내용을 입력해주세요");
} else {
axios
.post(
`http://3.39.223.175/api/board/${boardId}/comment/`,
{ comment: newComment },
{
headers: {
"Content-Type": `application/json`,
Authorization: `Bearer ${currentKey}`,
},
}
)
.then((response) => {
const nicknameData = response.data.createComment.nickname;
const commentData = response.data.createComment.comment;
setCommentArray((commentValueList) => [
...commentValueList,
{
nickname: nicknameData,
comment: commentData,
},
]);
})
.catch((error) => {
console.log(error);
});
}
setNewComment("");
};
return (
...
)
};
문제
댓글 달기
axios를 통해 받아온 객체 데이터를 어떻게 state 값에 업데이트 할까?
조건
- 댓글의 내역을 모두 조회할 수 있어야 합니다
'React' 카테고리의 다른 글
업로드 할 파일의 이미지 미리보기 (0) | 2022.06.20 |
---|---|
프론트에서 안전하게 로그인 요청하기(feat. 리액트) (0) | 2022.06.19 |
Redux + Firebase 게시글 화면에 뿌려주기(실패ver.) (0) | 2022.06.10 |
리액트 기초편(1) useEffect [❓] (0) | 2022.05.26 |
SPA와 MPA (0) | 2022.05.23 |