인프런 커뮤니티 질문&답변
질문있습니다.
해결된 질문
작성
·
245
0
질문1.
commentForm.js에서 댓글을 작성하고 버튼을 누르면 밑의 코드가 실행됩니다.
const onSubmitComment = useCallback(() => {
dispatch({
type: ADD_COMMENT_REQUEST,
data: { content: commentText, postId: post.id, userId: id },
});
}, [commentText, id]);
여기서 인자로 userId를 보내주었는데 리듀서와 사가를 거쳐 백엔드에서
router.post('/:postId/comment', isLoggedIn, async (req, res, next) => { // POST /post/1/comment
try {
const post = await Post.findOne({
where: { id: req.params.postId },
});
if (!post) {
return res.status(403).send('존재하지 않는 게시글입니다.');
}
const comment = await Comment.create({
content: req.body.content,
PostId: parseInt(req.params.postId, 10),
UserId: req.user.id,
})
const fullComment = await Comment.findOne({
where: { id: comment.id },
include: [{
model: User,
attributes: ['id', 'nickname'],
}],
})
// console.log("fullComment::::",JSON.stringify(fullComment),"enddddd");
res.status(201).json(fullComment);
} catch (error) {
console.error(error);
next(error);
}
});
위의 라우터가 실행되잖아요? 그런데 여기서 인자로 보내준 userId는 안쓰이고 req.user.id즉 세션에 저장되어 있는것을 쓰셨는데 그렇다면 위에서 userId를 인자로 넘겨준 이유가 무엇인지 궁금합니다.
질문2.
FollowButton.js입니다
팔로우 버튼을 누르면
const FollowButton = ({ post }) => {
const dispatch = useDispatch();
const { me, followLoading, unfollowLoading } = useSelector((state) => state.user);
const isFollowing = me?.Followings.find((v) => v.id === post.User.id);
console.log(isFollowing);
const onClickButton = useCallback(() => {
if (isFollowing) {
dispatch({
type: UNFOLLOW_REQUEST,
data: post.User.id,
});
} else {
dispatch({
type: FOLLOW_REQUEST,
data: post.User.id,
});
}
}, [isFollowing]);
if (post.User.id === me.id) {
return null;
}
return (
<Button loading={followLoading || unfollowLoading} onClick={onClickButton}>
{isFollowing ? '언팔로우' : '팔로우'}
</Button>
);
};
위의 코드가 실행되잖아요? 그런데 이때 console.log(isFollowing);으로 로그를 찍어 보았습니다.
그런데 팔로우 버튼을 누르면 console에서
이렇게 아이디만 뜨고 다시 새로고침을 해주어야
아이디와 아이디에 속해있는 Follow 객체를 불러오는데요... 왜 팔로우 버튼을 누른 후에는 (새로고침 하기전에는 )
id만 받아 오는것인지 궁금합니다!




