T
TanStack11mo ago
optimistic-gold

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;
}),
];
});
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.
1 Reply
optimistic-gold
optimistic-goldOP11mo ago
A little extra color: I'm just trying to employ this pattern for trimming pages off my infinite queries
TanStack | High Quality Open-Source Software for Web Developers
Headless, type-safe, powerful utilities for complex workflows like Data Management, Data Visualization, Charts, Tables, and UI Components.
From An unknown user
From An unknown user

Did you find this page helpful?