T
TanStack•3y ago
fair-rose

Return type of refetch includes loading states

👋 working with the refetch function returned by useQuery and noticed that the Promise data returned is a full QueryObserverResult, including loading states. Unless I'm missing something, I'd intuitively expect this to only return error or success states since, by the time the refetch Promise is resolved, the query should have finished loading. This has the tangible effects of status including the loading state as well as data including undefined in the union with TData - not a huge deal, but does feel a bit unintuitive. Is there a reason for this? There's a good chance I'm missing something obvious here 😅 just wanted to check before raising a formal issue / PR. Thanks!
1 Reply
fair-rose
fair-roseOP•3y ago
I suppose undefined is reasonable for data, since refetch could error when the previous data was still undefined. Would something like this be okay, or is there a reason to use the full QueryObserverResult?
refetch: <TPageData>(options?: RefetchOptions & RefetchQueryFilters<TPageData>) => Promise<DefinedQueryObserverResult<TData, TError> | QueryObserverLoadingErrorResult<TData, TError>>;
refetch: <TPageData>(options?: RefetchOptions & RefetchQueryFilters<TPageData>) => Promise<DefinedQueryObserverResult<TData, TError> | QueryObserverLoadingErrorResult<TData, TError>>;
(for reference, I'm on @tanstack/react-query 4.24.9, though at a glance it looks like the types are more or less the same in 5.0.0-alpha.24)

Did you find this page helpful?