-
Given the following code: export const createMutation: UseMutationOptions<
Todo,
unknown,
{ title: string; completed?: boolean },
{ optimisticTodo: Todo }
> = {
mutationKey: 'todos',
mutationFn: create,
onMutate: async (todo) => {
await queryClient.cancelQueries("todos");
const optimisticTodo: Todo = {
id: uuid(),
title: todo.title,
completed: todo.completed ?? false,
};
queryClient.setQueryData<Todo[]>("todos", (todos = []) => [
...todos,
optimisticTodo,
]);
return { optimisticTodo };
},
onSuccess: (newTodo, _, ctx) => {
queryClient.setQueryData<Todo[]>("todos", (todos = []) => {
return todos.map((todo) =>
todo.id === ctx?.optimisticTodo.id ? newTodo : todo
);
});
},
onError: (error, _, context) => {
queryClient.setQueryData<Todo[]>("todos", (todos = []) =>
todos.filter((todo) => todo.id !== context?.optimisticTodo?.id)
);
},
onSettled: () => {
queryCache.invalidateQueries('todos')
}
}; I end up with the following behavior:
Is it possible to block any We can only say |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 10 replies
-
Facing a similar issue... |
Beta Was this translation helpful? Give feedback.
-
I think the best way would be to disable your query if a mutation that could affect it is currently running. you can use the
keys for a more general but similar approach would be to disable queries globally if any mutation is running |
Beta Was this translation helpful? Give feedback.
-
My solution based on this discussion was to add this to
|
Beta Was this translation helpful? Give feedback.
-
no, it won't. I don't think it would solve the underlying problem, which is:
unless the second mutations double-locks, we would still invalidate. basically, we have been doing this in user-land lately:
with that, we can do:
this one also works, but you'd have to assign keys to all mutations. The |
Beta Was this translation helpful? Give feedback.
no, it won't. I don't think it would solve the underlying problem, which is:
unless the second mutations double-locks, we would still invalidate.
basically, we have been doing this in user-land lately: