MCPcopy Create free account
hub / github.com/TanStack/query / useBaseQuery

Function useBaseQuery

packages/react-query/src/useBaseQuery.ts:27–174  ·  view source on GitHub ↗
(
  options: UseBaseQueryOptions<
    TQueryFnData,
    TError,
    TData,
    TQueryData,
    TQueryKey
  >,
  Observer: typeof QueryObserver,
  queryClient?: QueryClient,
)

Source from the content-addressed store, hash-verified

25import type { UseBaseQueryOptions } from './types'
26
27export function useBaseQuery<
28 TQueryFnData,
29 TError,
30 TData,
31 TQueryData,
32 TQueryKey extends QueryKey,
33>(
34 options: UseBaseQueryOptions<
35 TQueryFnData,
36 TError,
37 TData,
38 TQueryData,
39 TQueryKey
40 >,
41 Observer: typeof QueryObserver,
42 queryClient?: QueryClient,
43): QueryObserverResult<TData, TError> {
44 if (process.env.NODE_ENV !== 'production') {
45 if (typeof options !== 'object' || Array.isArray(options)) {
46 throw new Error(
47 'Bad argument type. Starting with v5, only the "Object" form is allowed when calling query related functions. Please use the error stack to find the culprit call. More info here: https://tanstack.com/query/latest/docs/react/guides/migrating-to-v5#supports-a-single-signature-one-object',
48 )
49 }
50 }
51
52 const isRestoring = useIsRestoring()
53 const errorResetBoundary = useQueryErrorResetBoundary()
54 const client = useQueryClient(queryClient)
55 const defaultedOptions = client.defaultQueryOptions(options)
56 ;(client.getDefaultOptions().queries as any)?._experimental_beforeQuery?.(
57 defaultedOptions,
58 )
59
60 const query = client
61 .getQueryCache()
62 .get<
63 TQueryFnData,
64 TError,
65 TQueryData,
66 TQueryKey
67 >(defaultedOptions.queryHash)
68
69 if (process.env.NODE_ENV !== 'production') {
70 if (!defaultedOptions.queryFn) {
71 console.error(
72 `[${defaultedOptions.queryHash}]: No queryFn was passed as an option, and no default queryFn was found. The queryFn parameter is only optional when using a default queryFn. More info here: https://tanstack.com/query/latest/docs/framework/react/guides/default-query-function`,
73 )
74 }
75 }
76
77 const subscribed = options.subscribed !== false
78
79 // Make sure results are optimistically set in fetching state before subscribing or updating options
80 defaultedOptions._optimisticResults = isRestoring
81 ? 'isRestoring'
82 : subscribed
83 ? 'optimistic'
84 : undefined

Callers 4

useQueryFunction · 0.90
useSuspenseInfiniteQueryFunction · 0.90
useInfiniteQueryFunction · 0.90
useSuspenseQueryFunction · 0.90

Calls 15

useIsRestoringFunction · 0.90
useQueryClientFunction · 0.90
ensureSuspenseTimersFunction · 0.90
shouldSuspendFunction · 0.90
fetchOptimisticFunction · 0.90
getHasErrorFunction · 0.90
willFetchFunction · 0.90
defaultQueryOptionsMethod · 0.80
getDefaultOptionsMethod · 0.80

Tested by

no test coverage detected