Skip to content

Instantly share code, notes, and snippets.

@osamajavaid
Created September 15, 2023 06:41
Show Gist options
  • Save osamajavaid/c52c5f8d46d1ef0122c5f2611b1b0516 to your computer and use it in GitHub Desktop.
Save osamajavaid/c52c5f8d46d1ef0122c5f2611b1b0516 to your computer and use it in GitHub Desktop.
React Query: Invalidate multiple queries
import { useQuery, useQueryClient } from 'react-query';
// Inside your component or function...
const queryClient = useQueryClient();
// Example: Invalidate a single query
queryClient.invalidateQueries('yourQueryKey');
// Example: Invalidate multiple queries
queryClient.invalidateQueries(['queryKey1', 'queryKey2', 'queryKey3']);
// You can also pass additional options as the second argument
queryClient.invalidateQueries('yourQueryKey', {
refetchActive: true, // Refetch currently active queries
refetchInactive: true, // Refetch inactive queries as well
throwOnError: true, // Throw an error if the query fails to refetch
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment