T
TanStack15mo ago
inland-turquoise

Question about useQuery definition

Hello, Can someone explain to me what "The last successfully resolved data for the query" means? I read it on the docs about useQuery data property: https://tanstack.com/query/v4/docs/framework/react/reference/useQuery#:~:text=The%20last%20successfully%20resolved%20data%20for%20the%20query.
4 Replies
optimistic-gold
optimistic-gold15mo ago
Do you have a more specific question? Are you asking for an example of a time that the last request would have an error, so the phrase "successfully resolved" part matters?
inland-turquoise
inland-turquoiseOP15mo ago
For example, if I call my custom hook that calls useQuery that is related to myKey queryKey on screen A and then I call the same custom hook on screen B and need to use the values that data returns, it means that data will store the data already stored when I called the custom hook on screen A?
optimistic-gold
optimistic-gold15mo ago
I think you're asking if mounting a new component on screen B that subscribes to the same query key will cause the query to be refetched if the query key was already subscribed to on screen A by another useQuery? Am I understanding right? By default the mounting of a new component that uses (your custom hook that calls) useQuery for the same query key wil result in that data being fetched again. Whether this data is refetched depends on staleTime, but if you don't set it then the default is zero and every time a new useQuery() for the same query key mounts a new request will be sent. Once that request finishes successfully, every location where that query key is used will be updated.
inland-turquoise
inland-turquoiseOP15mo ago
I understand, thanks And in this example: on screen A I call my custom hook that calls useQuery associated with myKey queryKey and on screen B I set a variable const myData = queryClient.getQueryData(myKey). That means that myData will store the data fetched on screen A?

Did you find this page helpful?