좋아요 기능 비동기를 리액트 쿼리로

0
// -----------------------댓글 추가 기능----------------------------

   // 댓글 추가 기능
      const handleCommentSubmit = async (e) => {
      e.preventDefault();
     	try {
        // 댓글 작성 요청
        const accessToken = getCookie("accessToken");
        console.log(accessToken);
       	await instance.post(
             `/api/comments`,
             {
             postId: id,
              content: commentText
            },
             {
              headers: {
                    Accept: "*/*",
                     Authorization: `${accessToken}`,
                 },
            }
         )

         // 댓글 작성 후 폼 초기화
         setCommentText('');
    } catch (error) {
         console.log('댓글 작성 실패', error);
    }
}

-----------------------댓글 추가 기능----------------------------

const accessToken = getCookie("accessToken");



const addCommentMutation = useMutation(
    (commentText) =>
      instance.post(
        `/api/comments`,
        {
          postId: id,
          content: commentText,
        },
        {
          headers: {
            Accept: '*/*',
            Authorization: `${accessToken}`,
          },
        }
      ),
    {
      onError: (error) => {
        console.log('댓글 작성 실패', error);
      },

      onSettled: () => {
        queryClient.invalidateQueries('comments');
      },
    }
  );


  const handleCommentSubmit = async (e) => {
    e.preventDefault();
    try {
      await addCommentMutation.mutateAsync(commentText);
      

      setCommentText('');
    } catch (error) {
      console.log('댓글 작성 실패', error);
    }
  };
profile
𝙸 𝚊𝚖 𝚊 𝚌𝚞𝚛𝚒𝚘𝚞𝚜 𝚍𝚎𝚟𝚎𝚕𝚘𝚙𝚎𝚛 𝚠𝚑𝚘 𝚎𝚗𝚓𝚘𝚢𝚜 𝚍𝚎𝚏𝚒𝚗𝚒𝚗𝚐 𝚊 𝚙𝚛𝚘𝚋𝚕𝚎𝚖. 🇰🇷👩🏻‍💻

0개의 댓글