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

Function useQueries

packages/preact-query/src/useQueries.ts:303–440  ·  view source on GitHub ↗
(
  {
    queries,
    ...options
  }: {
    /**
     * An array with query option objects, mostly identical to `useQuery` — except that `queryClient` and
     * `subscribed` aren't accepted per-query (`subscribed` is a top-level option here instead), and
     * `placeholderData` accepts a {@link QueriesPlaceholderDataFunction}, which is called with `previousData`
     * and `previousQuery` always `undefined`, rather than `useQuery`'s placeholder function.
     */
    queries:
      | readonly [...QueriesOptions<T>]
      | readonly [...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]> }]
    /**
     * Use this to combine the results of the queries into a single value. The result will be structurally
     * shared to be as referentially stable as possible.
     */
    combine?: (result: QueriesResults<T>) => TCombinedResult
    /**
     * Set this to `false` to unsubscribe this observer from updates to the query cache.
     * @defaultValue true
     */
    subscribed?: boolean
  },
  queryClient?: QueryClient,
)

Source from the content-addressed store, hash-verified

301 * ```
302 */
303export function useQueries<
304 T extends Array<any>,
305 TCombinedResult = QueriesResults<T>,
306>(
307 {
308 queries,
309 ...options
310 }: {
311 /**
312 * An array with query option objects, mostly identical to `useQuery` — except that `queryClient` and
313 * `subscribed` aren't accepted per-query (`subscribed` is a top-level option here instead), and
314 * `placeholderData` accepts a {@link QueriesPlaceholderDataFunction}, which is called with `previousData`
315 * and `previousQuery` always `undefined`, rather than `useQuery`'s placeholder function.
316 */
317 queries:
318 | readonly [...QueriesOptions<T>]
319 | readonly [...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]> }]
320 /**
321 * Use this to combine the results of the queries into a single value. The result will be structurally
322 * shared to be as referentially stable as possible.
323 */
324 combine?: (result: QueriesResults<T>) => TCombinedResult
325 /**
326 * Set this to `false` to unsubscribe this observer from updates to the query cache.
327 * @defaultValue true
328 */
329 subscribed?: boolean
330 },
331 queryClient?: QueryClient,
332): TCombinedResult {
333 const client = useQueryClient(queryClient)
334 const isRestoring = useIsRestoring()
335 const errorResetBoundary = useQueryErrorResetBoundary()
336 const subscribed = options.subscribed !== false
337
338 const defaultedQueries = useMemo(
339 () =>
340 queries.map((opts) => {
341 const defaultedOptions = client.defaultQueryOptions(
342 opts as QueryObserverOptions,
343 )
344
345 // Make sure the results are already in fetching state before subscribing or updating options
346 defaultedOptions._optimisticResults = isRestoring
347 ? 'isRestoring'
348 : subscribed
349 ? 'optimistic'
350 : undefined
351
352 return defaultedOptions
353 }),
354 [queries, client, isRestoring, subscribed],
355 )
356
357 defaultedQueries.forEach((queryOptions) => {
358 ensureSuspenseTimers(queryOptions)
359 const query = client.getQueryCache().get(queryOptions.queryHash)
360 ensurePreventErrorBoundaryRetry(queryOptions, errorResetBoundary, query)

Callers 10

useSuspenseQueriesFunction · 0.90
useCustomQueriesFunction · 0.90
PageFunction · 0.90
useWrappedQueriesFunction · 0.90
TestComponentFunction · 0.90
CharactersFunction · 0.90
PageFunction · 0.50
PageFunction · 0.50

Calls 15

useQueryClientFunction · 0.90
useIsRestoringFunction · 0.90
ensureSuspenseTimersFunction · 0.90
useSyncExternalStoreFunction · 0.90
shouldSuspendFunction · 0.90
fetchOptimisticFunction · 0.90
getHasErrorFunction · 0.90
defaultQueryOptionsMethod · 0.80
getMethod · 0.80

Tested by 6

useCustomQueriesFunction · 0.72
PageFunction · 0.72
useWrappedQueriesFunction · 0.72
TestComponentFunction · 0.72
PageFunction · 0.40
PageFunction · 0.40