Skip to content

Commit e34baf9

Browse files
committed
more optimistic update fixes
1 parent c452803 commit e34baf9

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

apps/3.1-vite-reactquery-refactor/src/pages/PostPage.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const PostPage = () => {
6464
`http://localhost:3333/comments/${commentId}`,
6565
{
6666
method: "DELETE",
67-
},
67+
}
6868
);
6969
return response.json() as Promise<IComment>;
7070
},
@@ -73,7 +73,7 @@ export const PostPage = () => {
7373
onError: (err, commentId) => {
7474
console.error(
7575
`Error deleting comment ${commentId}. Rolling UI back`,
76-
err,
76+
err
7777
);
7878
alert("Error deleting comment");
7979
},
@@ -101,7 +101,7 @@ export const PostPage = () => {
101101
//optimistic client-side update
102102
onMutate: async (newComment) => {
103103
await queryClient.cancelQueries({
104-
queryKey: ["comments", newComment.postId.toString()],
104+
queryKey: getPostCommentsQueryKey(postId!),
105105
});
106106

107107
// Snapshot the previous value
@@ -112,19 +112,19 @@ export const PostPage = () => {
112112

113113
// Optimistically update to the new value
114114
queryClient.setQueryData(
115-
["comments", newComment.postId.toString()],
116-
(oldComments: any) => [...oldComments, newComment],
115+
getPostCommentsQueryKey(postId!),
116+
(oldComments: any) => [...oldComments, newComment]
117117
);
118118

119119
// Return a context object with the snapshot value
120120
return { previousComments };
121121
},
122122
// If the mutation fails,
123123
// use the context returned from onMutate to roll back
124-
onError: (err, newComment, context) => {
124+
onError: (err, _newComment, context) => {
125125
queryClient.setQueryData(
126-
["comments", newComment.postId.toString()],
127-
context?.previousComments,
126+
getPostCommentsQueryKey(postId!),
127+
context?.previousComments
128128
);
129129
console.error("Error posting comment. Rolling UI back", err);
130130
},

0 commit comments

Comments
 (0)