Skip to content

Does invalidateQueries accept a list of query keys? #10612

@dev-mashael-otb

Description

@dev-mashael-otb

Describe the bug

Hi ,

when I tried to invalidate multiple queries like this:

queryClient.invalidateQueries({ queryKey: ['queryKeyOne', 'queryKeyTwo'] });

it’s not working ... it does not invalidate both queries

so is there any solution to invalidate multiple query keys?

Your minimal, reproducible example

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';

function Example() {
const queryClient = useQueryClient();

useQuery({ queryKey: ['queryKeyOne'],
queryFn: () => Promise.resolve('data1'), });

useQuery({ queryKey: ['queryKeyTwo'],
queryFn: () => Promise.resolve('data2'), });

const mutation = useMutation({
mutationFn: () => Promise.resolve(true),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['queryKeyOne', 'queryKeyTwo'] });
}, });

return (
<button onClick={() => mutation.mutate()}> invalidateQueries
); }

Steps to reproduce

  1. Create two queries with different keys:

    • ['queryKeyOne']
    • ['queryKeyTwo']
  2. Call:
    queryClient.invalidateQueries({ queryKey: ['queryKeyOne', 'queryKeyTwo'] });

  3. Check the network or React Query Devtools

  4. Notice that neither query is invalidated

Expected behavior

Both queries ['queryKeyOne'] and ['queryKeyTwo'] should be invalidated when calling

queryClient.invalidateQueries({ queryKey: ['queryKeyOne', 'queryKeyTwo'] });

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

macOS

Tanstack Query adapter

@tanstack/react-query

TanStack Query version

"@tanstack/react-query": "^5.17.9"

TypeScript version

"typescript": "^5"

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions