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

Function useQueries

packages/react-query/src/useQueries.ts:355–493  ·  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

353 * ```
354 */
355export function useQueries<
356 T extends Array<any>,
357 TCombinedResult = QueriesResults<T>,
358>(
359 {
360 queries,
361 ...options
362 }: {
363 /**
364 * An array with query option objects, mostly identical to `useQuery` — except that `queryClient` and
365 * `subscribed` aren't accepted per-query (`subscribed` is a top-level option here instead), and
366 * `placeholderData` accepts a {@link QueriesPlaceholderDataFunction}, which is called with `previousData`
367 * and `previousQuery` always `undefined`, rather than `useQuery`'s placeholder function.
368 */
369 queries:
370 | readonly [...QueriesOptions<T>]
371 | readonly [...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]> }]
372 /**
373 * Use this to combine the results of the queries into a single value. The result will be structurally
374 * shared to be as referentially stable as possible.
375 */
376 combine?: (result: QueriesResults<T>) => TCombinedResult
377 /**
378 * Set this to `false` to unsubscribe this observer from updates to the query cache.
379 * @defaultValue true
380 */
381 subscribed?: boolean
382 },
383 queryClient?: QueryClient,
384): TCombinedResult {
385 const client = useQueryClient(queryClient)
386 const isRestoring = useIsRestoring()
387 const errorResetBoundary = useQueryErrorResetBoundary()
388 const subscribed = options.subscribed !== false
389
390 const defaultedQueries = React.useMemo(
391 () =>
392 queries.map((opts) => {
393 const defaultedOptions = client.defaultQueryOptions(
394 opts as QueryObserverOptions,
395 )
396
397 // Make sure the results are already in fetching state before subscribing or updating options
398 defaultedOptions._optimisticResults = isRestoring
399 ? 'isRestoring'
400 : subscribed
401 ? 'optimistic'
402 : undefined
403
404 return defaultedOptions
405 }),
406 [queries, client, isRestoring, subscribed],
407 )
408
409 defaultedQueries.forEach((queryOptions) => {
410 ensureSuspenseTimers(queryOptions)
411 const query = client.getQueryCache().get(queryOptions.queryHash)
412 ensurePreventErrorBoundaryRetry(queryOptions, errorResetBoundary, query)

Callers 15

Example2Function · 0.90
Example3Function · 0.90
Example4Function · 0.90
Example5Function · 0.90
Example2Function · 0.90
Example3Function · 0.90
Example4Function · 0.90
Example5Function · 0.90
ExamplesFunction · 0.90
ExamplesFunction · 0.90
ExamplesFunction · 0.90
ExamplesFunction · 0.90

Calls 15

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

Tested by 9

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