Is there a way to have the refetch function automatically cancel previously running queries? #1636
Answered
by
ashwinGokhale
ashwinGokhale
asked this question in
General
-
Is there a way to have the refetch function automatically cancel previously running queries? For example, you have a button to refetch posts, and every time you click, you want to cancel any previous queries to fetch posts. const queryKey = 'posts';
const client = useQueryClient();
const {refetch} = useQuery(queryKey, fetchPosts);
const refreshPosts = () => {
client.cancelQueries(queryKey);
refetch();
}
return <button onClick={refreshPosts}>Refresh posts</button> Is there an option to have react-query automatically handle cancelling the previous query for me? Something like this: const client = useQueryClient();
const {refetch} = useQuery('posts', fetchPosts);
return <button onClick={() => refetch({cancelPrevious: true})}>Refresh posts</button> Or something like this: const client = useQueryClient();
const {refetch} = useQuery('posts', fetchPosts, {cancelPrevious: true});
return <button onClick={refetch}>Refresh posts</button> |
Beta Was this translation helpful? Give feedback.
Answered by
ashwinGokhale
Jan 17, 2021
Replies: 1 comment 2 replies
-
To answer my own question, I looked into the source code a bit and found that you can use |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
ashwinGokhale
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To answer my own question, I looked into the source code a bit and found that you can use
refetch({cancelRefetch: true})}
. However, this is not exposed in the typescript typings.