Skip to content

Commit

Permalink
feat: (#55) useMutation 함수 반환값으로 isLoading, isError, error 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
inyeong-kang committed Jul 18, 2023
1 parent 292f462 commit 56a29e0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
23 changes: 13 additions & 10 deletions frontend/src/hooks/query/post/useCreatePost.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { QUERY_KEY } from '@constants/queryKey';
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { PostRequest } from '@type/post';
Expand All @@ -6,15 +7,17 @@ import { createPost } from '@api/jero/post';

export const useCreatePost = () => {
const queryClient = useQueryClient();
const { mutate } = useMutation((post: PostRequest) => createPost(post), {
onSuccess: () => {
queryClient.invalidateQueries(['posts']);
},
onError: () => {
// mutate가 실패하면, 함수를 실행합니다.
window.console.log('createPost error');
},
});
const { mutate, isLoading, isError, error } = useMutation(
(post: PostRequest) => createPost(post),
{
onSuccess: () => {
queryClient.invalidateQueries([QUERY_KEY.posts]);
},
onError: () => {
window.console.log('createPost error');
},
}
);

return { mutate };
return { mutate, isLoading, isError, error };
};
22 changes: 13 additions & 9 deletions frontend/src/hooks/query/post/useEditPost.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { QUERY_KEY } from '@constants/queryKey';
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { PostRequest } from '@type/post';
Expand All @@ -6,14 +7,17 @@ import { editPost } from '@api/jero/post';

export const useEditPost = (postId: number) => {
const queryClient = useQueryClient();
const { mutate } = useMutation((updatedPost: PostRequest) => editPost(postId, updatedPost), {
onSuccess: () => {
queryClient.invalidateQueries(['posts', postId]);
},
onError: () => {
window.console.log('editPost error');
},
});
const { mutate, isLoading, isError, error } = useMutation(
(updatedPost: PostRequest) => editPost(postId, updatedPost),
{
onSuccess: () => {
queryClient.invalidateQueries([QUERY_KEY.posts, postId]);
},
onError: () => {
window.console.log('editPost error');
},
}
);

return { mutate };
return { mutate, isLoading, isError, error };
};

0 comments on commit 56a29e0

Please sign in to comment.