Open History to see the last commit
1'use client'
2import * as React from 'react'
3β
4import { noop, notifyManager } from '@tanstack/query-core'
5import { useQueryClient } from './QueryClientProvider'
6import { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'
7import {
8 ensurePreventErrorBoundaryRetry,
9 getHasError,
10 useClearResetErrorBoundary,
11} from './errorBoundaryUtils'
12import { useIsRestoring } from './IsRestoringProvider'
13import {
14 ensureSuspenseTimers,
15 fetchOptimistic,
16 shouldSuspend,
17} from './suspense'
18import type {
19 QueryClient,
20 QueryKey,
21 QueryObserver,
22 QueryObserverResult,
23} from '@tanstack/query-core'
24import type { UseBaseQueryOptions } from './types'
25β
26export function useBaseQuery<
27 TQueryFnData,
28 TError,
29 TData,
30 TQueryData,
31 TQueryKey extends QueryKey,
32>(
33 options: UseBaseQueryOptions<
34 TQueryFnData,
35 TError,
36 TData,
37 TQueryData,
38 TQueryKey
39 >,
40 Observer: typeof QueryObserver,
41 queryClient?: QueryClient,
42): QueryObserverResult<TData, TError> {
43 if (process.env.NODE_ENV !== 'production') {
44 if (typeof options !== 'object' || Array.isArray(options)) {
45 throw new Error(
46 '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',
47 )
48 }
49 }
50β
51 const isRestoring = useIsRestoring()
52 const errorResetBoundary = useQueryErrorResetBoundary()
53 const client = useQueryClient(queryClient)
54 const defaultedOptions = client.defaultQueryOptions(options)
55β
56 const query = client
57 .getQueryCache()
58 .get<TQueryFnData, TError, TQueryData, TQueryKey>(
59 defaultedOptions.queryHash,
60 )
61β
62 if (process.env.NODE_ENV !== 'production') {
63 if (!defaultedOptions.queryFn) {
64 console.error(
65 `[${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`,
66 )
67 }
68 }
69β
70 const subscribed = options.subscribed !== false
71β
72 // Make sure results are optimistically set in fetching state before subscribing or updating options
73 defaultedOptions._optimisticResults = isRestoring
74 ? 'isRestoring'
75 : subscribed
76 ? 'optimistic'
77 : undefined
78β
79 ensureSuspenseTimers(defaultedOptions)
80 ensurePreventErrorBoundaryRetry(defaultedOptions, errorResetBoundary, query)
81 useClearResetErrorBoundary(errorResetBoundary)
82β
83 const [observer] = React.useState(
84 () =>
85 new Observer<TQueryFnData, TError, TData, TQueryData, TQueryKey>(
86 client,
87 defaultedOptions,
88 ),
89 )
90β
91 // note: this must be called before useSyncExternalStore
92 const result = observer.getOptimisticResult(defaultedOptions)
93β
94 const shouldSubscribe = !isRestoring && subscribed
95 React.useSyncExternalStore(
96 React.useCallback(
97 (onStoreChange) => {
98 const unsubscribe = shouldSubscribe
99 ? observer.subscribe(notifyManager.batchCalls(onStoreChange))
100 : noop
101β
102 // Update result to make sure we did not miss any query updates
103 // between creating the observer and subscribing to it.
104 observer.updateResult()
105β
106 return unsubscribe
107 },
108 [observer, shouldSubscribe],
109 ),
110 () => observer.getCurrentResult(),
111 () => observer.getCurrentResult(),
112 )
113β
114 React.useEffect(() => {
115 observer.setOptions(defaultedOptions)
116 }, [defaultedOptions, observer])
117β
118 // Handle suspense
119 if (shouldSuspend(defaultedOptions, result)) {
120 throw fetchOptimistic(defaultedOptions, observer, errorResetBoundary)
121 }
122β
123 // Handle error boundary
124 if (
125 getHasError({
126 result,
127 errorResetBoundary,
128 throwOnError: defaultedOptions.throwOnError,
129 query,
130 suspense: defaultedOptions.suspense,
131 })
132 ) {
133 throw result.error
134 }
135β
136 // Handle result property usage tracking
137 return !defaultedOptions.notifyOnChangeProps
138 ? observer.trackResult(result)
139 : result
140}