How exactly is "previousData" in "placeholderData" defined?
In "Placeholder Data as a Function" (https://tanstack.com/query/latest/docs/framework/react/guides/placeholder-query-data#placeholder-data-as-a-function) it says:
placeholderData can also be a function, where you can get access to the data and Query meta information of a "previous" successful QueryI noticed, that when I run a query with key
["data", "1"]
and then a 2. query with key ["data", "2"]
, the second query gets the data from the first one, even they have different query keys. If run the second query with key ["data" ]
, I don't get the data from the first query.
So my question is, how is "previous" query defined here (I guess the quotes in the docs indicate, that there is a special meaning of "previous" here 😉 )Placeholder Query Data | TanStack Query React Docs
What is placeholder data? Placeholder data allows a query to behave as if it already has data, similar to the initialData option, but the data is not persisted to the cache. This comes in handy for si...
7 Replies
puzzled-coral•9mo ago
The data from the last successful fetch is available while new data is being requested, even though the query key has changed.Would the query that has the query ket
["data"]
fit the same queryFn
?genetic-orangeOP•9mo ago
No in my example
[data]
is a differnt queryFn (two components each having their own useQuery
)
So previous
referes to the previous query executed using the same queryFn
?puzzled-coral•9mo ago
Previous successful one 👍
genetic-orangeOP•9mo ago
I only run successful queries 😉 Thanks for clarification!
genetic-orange•9mo ago
no 🙂
when you change keys in the same observer (= useQuery "instance"), you can get access to the data from the query you were previously observing
I noticed, that when I run a query with keyShould work for both cases, as it doesn't mattet which key you change towards.["data", "1"]
and then a 2. query with key["data", "2"]
, the second query gets the data from the first one, even they have different query keys. If run the second query with key["data" ]
, I don't get the data from the first query.
fair-rose•9mo ago
So the observer tracks key changes?
genetic-orange•9mo ago
yes, the observer sees that it switches between queries, but it still has access to the previous one it was tracking