|
Currently, the staleTime option in useQuery is determined when the query is initially created and cannot be modified later. However, there are scenarios where the optimal staleTime depends on the response from the query itself (for example, when the API provides a recommended caching duration). It would be highly beneficial if staleTime could be updated dynamically based on the data returned from the query. Current Behavior:
Desired Behavior:
Possible Solutions:
useQuery({
queryKey: ['my-query'],
queryFn: fetchMyData,
staleTime: (data) => data.timeout
})Why This Feature Is Needed: Is there a best practice for this problem? |
Replies: 2 comments 4 replies
|
|
|
I couldn't find a I came up with a workaround: const qc = useQueryClient();
useQuery({
staleTime: ({ queryKey }) => {
const data = qc.getQueryData(queryKey);
// ...
return staleTime;
}
}); |
staleTimecan already be a function that receives the latestquery, so you can build this already.