Manual Query Cancellation not working #7962
-
In Query cancellation, I tried to cancel the query by manual by referring your code in but it doesn't work properly. but i tired it different way then also it doesn't work. can you give correct code regarding that with working. and my code is
while using this queryClient.cancelQueries(['todos']), it is not throwing me the error inside the catch block. give me an example regarding this |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hello In your example, there is 2 errors for me:
const { isLoading, isError, isSuccess, data, error } = useQuery({
queryKey: ['todos'],
// Get the signal to pass to the fetch
queryFn: async ({ signal }) => {
try {
const resp = await fetch(`https://jsonplaceholder.typicode.com/posts?_page=5&_limit=10`, { signal });
if (!resp.ok) {
throw new Error('Network response was not ok');
}
return resp.json();
} catch (err) {
throw err;
}
},
});
// Function to handle the button press and cancel the query
const handleCancel = () => {
// You need to pass an object
queryClient.cancelQueries({ queryKey: ['todos'] });
Alert.alert("Query cancelled");
}; You can see full example here https://tanstack.com/query/latest/docs/framework/react/guides/query-cancellation#manual-cancellation. |
Beta Was this translation helpful? Give feedback.
Hello
In your example, there is 2 errors for me:
queryFn
parameter:queryClient.cancelQueries
…