Skip to content
Source
Sign inSign up
TanStack/query

use setQueriesData with InfiniteQueries #9183

Send to my agent
Closedchris-acuvity(GitHub profile, opens in a new tab) opened this issue 3 comments

Describe the bug

Hi there,

I have been looking at the documentation and experimenting with setQueriesData using the latest version of React Query.

For some reason, the oldData is always undefined

Your minimal, reproducible example

See code below

Steps to reproduce

Here is an example of code:

queryClient.setQueriesData(
        {
          queryKey, // The query key correctly filters my queries
        },
        (oldData) => {
          // oldData is retrieve when the query has been made with useQuery but not useInfiniteQuery
          console.log(">>", queryKey, oldData); 

          // Do any immutable change here if needed
          return oldData
        }
      );

Expected behavior

I was expecting to get the data and be able to modify an object if the identifier matches

I am wondering if it's a known expectation as useInfiniteQuery returns an object containing the pages and the pageParams.

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

Chrome on macOS

Tanstack Query adapter

None

TanStack Query version

5.76.2

TypeScript version

6.15.0

Additional context

No response

  1. if oldData is undefined, then the queryKey didn’t match any existing data in the cache (it uses QueryFilters for matching). You need to show a complete reproduction to analyze this further please.

  2. TkDodo closed this

  3. TkDodo added labelsneeds-info

  4. Hey @TkDodo,

    We are really struggling with this issue. It looks super easy and maybe we are missing the obvious

    The queryKey has returned some data: Before calling setQueriesData, I am calling getQueriesData with the same query and I can find results.

    Please take a look at this small code extraction

    const queryKey = [
            message.identity,
            message.entity.namespace,
            "retrieve-infinite",
          ];
    
          const queries = queryClient.getQueriesData({ queryKey });
          console.log("message", message); <---- Show the WS message
          console.log("queries", queries); <---- Show the current queries that matches the key
    
          queryClient.setQueriesData(
            {
              queryKey,
            },
            (oldData) => {
              console.log("--------", oldData); <---- Show the oldData
              return oldData;
             }
           )
    }

    Here is the result with my comments

    Image

  5. TkDodocommented

    we have a test that does exactly this:

    https://github.com/TanStack/query/blob/b475d2156860243b5c57bf1190919dba67305586/packages/query-core/src/tests/queryClient.test.tsx#L351-L366

    so you really need to show me a stackblitz or codesandbox minimal reproduction where I can see that this doesn’t work

Sign in to join this conversation.