invaliateQueries race condition with refetchOnWindowFocus #5236
-
Hi folks, I'm building this list view where user can delete an item from the list by clicking on an "Delete" button. The list data is fetched with const listQuery = useQuery(['todos', filters]);
const deleteTodo = useMutation((id) => api.deleteTodo(id), {
onSuccess() {
queryClient.invalidateQueries(['todos'])
}
}) Everything works as expected except for an edge case:
Having no idea how to get around this. Any advice? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
it does since v4. An explicit call to If you don't want that, you can call |
Beta Was this translation helpful? Give feedback.
it does since v4. An explicit call to
invalidateQueries()
will cancel currently running requests and start a new one, to stop this exact issue.If you don't want that, you can call
invalidateQueries(['todos'], { cancelRefetch: false })
. If you are on v3, you can explicitly passcancelRefetch: true
, because there, the defaults were reversed:https://tanstack.com/query/v4/docs/react/guides/migrating-to-react-query-4#consistent-behavior-for-cancelrefetch