가수면
[React Query] useMutation 본문
async function deletePost(postId) {
const response = await fetch(`https://jsonplaceholder.typicode.com/postId/${postId}`, { method: "DELETE" });
return response.json();
}
const { data, isLoading, isError } = useQuery(["comments", post.id], () => fetchComments(post.id));
// useMutation
const deleteMutation = useMutation((postId) => {
deletePost(postId);
});
const updateMutation = useMutation(() => {
updatePost(post.id);
});
.
.
.
<button onClick={() => deleteMutation.mutate(post.id)}>Delete</button>
<button onClick={() => updateMutation.mutate()}>Update title</button>
{deleteMutation.isError && <p style={{ color: "red" }}>에러</p>}
{deleteMutation.isLoading && <p style={{ color: "purple" }}>로딩 중</p>}
{deleteMutation.isSuccess && <p style={{ color: "green" }}>삭제 됨</p>}
{updateMutation.isError && <p style={{ color: "red" }}>에러</p>}
{updateMutation.isLoading && <p style={{ color: "purple" }}>로딩 중</p>}
{updateMutation.isSuccess && <p style={{ color: "green" }}>수정 됨</p>}
'React > 라이브러리' 카테고리의 다른 글
Helmet (0) | 2023.01.09 |
---|---|
ApexCharts (0) | 2023.01.09 |
[React Query] isFetching vs isLoding (0) | 2023.01.06 |
[React Query] useQuery (0) | 2023.01.06 |
리액트 훅 폼 (useForm) (0) | 2023.01.03 |
Comments