How to update a list after mutating one of its records? #887
-
Hello, I've seen how the const updated = await updateTodo({
where: { id: todo.id },
data: { text },
})
mutate(updated) This will basically update the initial query payload updating our UI. However, with a list I assume it would be different when updating a single record from the list. For example: const [uncompletedTodos, { mutate: mutateUncompletedTodo }] = useQuery(getTodos, {
orderBy: { id: "desc" },
where: { completed: false },
})
const [completedTodos, { mutate: mutateCompletedTodo }] = useQuery(getTodos, {
orderBy: { updatedAt: "desc" },
where: { completed: true },
}) If I try to do the same update as the first code snippet, it will not be compatible since the returned data is a single object and the original query is an array of objects. I have a couple of ideas on how to I'm guessing one can do Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@johhansantana great question! We currently don't have anything special for this. So you'll need to update the array and then call I think immer would probably be useful here to create the new array data. Something like this: import produce from "immer"
const newUncompletedTodos = produce(uncompletedTodos, draft => {
draft.splice(indexOfCompletedTodo, 1)
})
mutate(newUncompletedTodos) |
Beta Was this translation helpful? Give feedback.
@johhansantana great question! We currently don't have anything special for this.
So you'll need to update the array and then call
mutate(completedTodos)
andmutate(uncompletedTodos)
.I think immer would probably be useful here to create the new array data. Something like this: