TanStackT
TanStack16mo ago
1 reply
slow-yellow

setQueryData does not pass existing data

I'm running into very strange behavior where calling setQueryData does not include the existing data in the updater callback.

    queryClient
      .getQueryCache()
      .findAll({ queryKey })
      .map(({ queryKey: key, state }) => {
        console.log({ key: JSON.stringify(key), data: Boolean(state.data) });
        return [
          key,
          queryClient.setQueryData(key, (oldData) => {
            console.log({ oldData: Boolean(oldData) });
            return oldData;
          }),
        ];
      });


In the above code, the first console.log (L5) includes data, but the oldData inside the setQueryData callback is undefined. I have this code running in two different places with identical invocations, just different queryKeys – one works as expected, the other doesn't.
Was this page helpful?