Updating a single item from the cache #474
-
Thank you for this amazing library! I just had one query. Is it possible to update a single item from a list by fetching it from the I have a snippet below to edit a todo. Would this be the best approach to do it? const [editTodo] = useMutation(edit, {
onMutate: (edited) => {
const previousTodos = queryCache.getQueryData('todos') as Todo[]
const updatedTodos = [...previousTodos]
const index = updatedTodos.findIndex((todo) => todo.id === edited.id)
if (index !== -1) {
updatedTodos[index] = {
...updatedTodos[index],
...edited.body,
}
queryCache.setQueryData('todos', updatedTodos)
}
return () => queryCache.setQueryData('todos', previousTodos)
},
}) |
Beta Was this translation helpful? Give feedback.
Answered by
tannerlinsley
May 9, 2020
Replies: 1 comment 5 replies
-
Yeah that should be fine |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
ryands17
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah that should be fine