import { UseSuspenseQueryOptions, UseSuspenseQueryResult } from "./types.js"; import { DefaultError, QueryClient, QueryFunction, ThrowOnError } from "@tanstack/query-core"; //#region src/useSuspenseQueries.d.ts type MAXIMUM_DEPTH = 20; type SkipTokenForUseQueries = symbol; type GetUseSuspenseQueryOptions = T extends { queryFnData: infer TQueryFnData; error?: infer TError; data: infer TData; } ? UseSuspenseQueryOptions : T extends { queryFnData: infer TQueryFnData; error?: infer TError; } ? UseSuspenseQueryOptions : T extends { data: infer TData; error?: infer TError; } ? UseSuspenseQueryOptions : T extends [infer TQueryFnData, infer TError, infer TData] ? UseSuspenseQueryOptions : T extends [infer TQueryFnData, infer TError] ? UseSuspenseQueryOptions : T extends [infer TQueryFnData] ? UseSuspenseQueryOptions : T extends { queryFn?: QueryFunction | SkipTokenForUseQueries; select?: (data: any) => infer TData; throwOnError?: ThrowOnError; } ? UseSuspenseQueryOptions : T extends { queryFn?: QueryFunction | SkipTokenForUseQueries; throwOnError?: ThrowOnError; } ? UseSuspenseQueryOptions : UseSuspenseQueryOptions; type GetUseSuspenseQueryResult = T extends { queryFnData: any; error?: infer TError; data: infer TData; } ? UseSuspenseQueryResult : T extends { queryFnData: infer TQueryFnData; error?: infer TError; } ? UseSuspenseQueryResult : T extends { data: infer TData; error?: infer TError; } ? UseSuspenseQueryResult : T extends [any, infer TError, infer TData] ? UseSuspenseQueryResult : T extends [infer TQueryFnData, infer TError] ? UseSuspenseQueryResult : T extends [infer TQueryFnData] ? UseSuspenseQueryResult : T extends { queryFn?: QueryFunction | SkipTokenForUseQueries; select?: (data: any) => infer TData; throwOnError?: ThrowOnError; } ? UseSuspenseQueryResult : T extends { queryFn?: QueryFunction | SkipTokenForUseQueries; throwOnError?: ThrowOnError; } ? UseSuspenseQueryResult : UseSuspenseQueryResult; /** * The `queries` array accepted by `useSuspenseQueries`. Recursively unwraps each tuple element so every * entry's `queryFn`/`select` are inferred individually, up to 20 elements. An opaque array (e.g. `unknown[]`) * is returned as-is; a non-tuple array of a known element type, or a tuple past 20 elements, falls back to a * single homogeneous {@link UseSuspenseQueryOptions} type. * * @template T - The type of the `queries` array as written at the call site. * @template TResults - The internal accumulator that this type builds during recursion. It is not meant * to be set explicitly. * @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not * meant to be set explicitly. */ type SuspenseQueriesOptions, TResults extends Array = [], TDepth extends ReadonlyArray = []> = TDepth['length'] extends MAXIMUM_DEPTH ? Array : T extends [] ? [] : T extends [infer Head] ? [...TResults, GetUseSuspenseQueryOptions] : T extends [infer Head, ...infer Tails] ? SuspenseQueriesOptions<[...Tails], [...TResults, GetUseSuspenseQueryOptions], [...TDepth, 1]> : Array extends T ? T : T extends Array> ? Array> : Array; /** * The result type returned by `useSuspenseQueries`, when no `combine` is provided. Mirrors * {@link SuspenseQueriesOptions}: each tuple element's result type is inferred individually, up to 20 elements. * A non-tuple array is mapped per-element instead, still inferring each entry individually; only past 20 * elements does this fall back to a single homogeneous {@link UseSuspenseQueryResult} type. * * @template T - The type of the `queries` array, as inferred by {@link SuspenseQueriesOptions}. * @template TResults - The internal accumulator that this type builds during recursion. It is not meant * to be set explicitly. * @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not * meant to be set explicitly. */ type SuspenseQueriesResults, TResults extends Array = [], TDepth extends ReadonlyArray = []> = TDepth['length'] extends MAXIMUM_DEPTH ? Array : T extends [] ? [] : T extends [infer Head] ? [...TResults, GetUseSuspenseQueryResult] : T extends [infer Head, ...infer Tails] ? SuspenseQueriesResults<[...Tails], [...TResults, GetUseSuspenseQueryResult], [...TDepth, 1]> : { [K in keyof T]: GetUseSuspenseQueryResult; }; /** * The options for `useSuspenseQueries` are the same as for `useQueries`, except that the top-level `subscribed` * option isn't supported, and each `query` can't have `throwOnError`, `enabled`, or `placeholderData`. * * @param options - The `queries` array to run in Suspense, and an optional `combine` function. * @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context * will be used. * @returns The same structure as `useQueries`, except that for each `query`, `data` is guaranteed to be * defined, `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived * flags set accordingly). * * Caveat: the component will only re-mount after all queries have finished loading. Hence, if a query has gone * stale in the time it took for all the queries to complete, it will be fetched again at re-mount. To avoid * this, make sure to set a high enough `staleTime`. Cancellation does not work. * * @example * The query error is thrown if a fetch fails and no cached data exists yet, so an error boundary is * required around ``. A failed background refetch instead continues to render the cached data. * Use {@link QueryErrorResetBoundary} to let the user retry after such an error: * ```tsx * import { Suspense } from 'react' * import { ErrorBoundary } from 'react-error-boundary' * import { * QueryErrorResetBoundary, * useSuspenseQueries, * } from '@tanstack/react-query' * * function Posts({ ids }: { ids: Array }) { * // Every result is guaranteed to be defined — no per-query `isPending` check needed. * const postQueries = useSuspenseQueries({ * queries: ids.map((id) => ({ * queryKey: ['post', id], * queryFn: () => fetchPost(id), * })), * }) * * return ( *
    * {postQueries.map((query) => ( *
  • {query.data.title}
  • * ))} *
* ) * } * * function App() { * return ( * * {({ reset }) => ( * ( *
* There was an error! * *
* )} * > * Loading posts...}> * * *
* )} *
* ) * } * ``` * * @example * Several different queries — use `useSuspenseQueries` instead of multiple `useSuspenseQuery` calls, so * they fetch in parallel rather than suspending one after another: * ```tsx * import { Suspense } from 'react' * import { ErrorBoundary } from 'react-error-boundary' * import { * QueryErrorResetBoundary, * useSuspenseQueries, * } from '@tanstack/react-query' * * function Dashboard() { * const [usersQuery, teamsQuery, projectsQuery] = useSuspenseQueries({ * queries: [ * { queryKey: ['users'], queryFn: fetchUsers }, * { queryKey: ['teams'], queryFn: fetchTeams }, * { queryKey: ['projects'], queryFn: fetchProjects }, * ], * }) * * return ( *
* * * *
* ) * } * * function App() { * return ( * * {({ reset }) => ( * ( *
* There was an error! * *
* )} * > * Loading dashboard...}> * * *
* )} *
* ) * } * ``` * * @example * `combine`s the results into a single boolean, so `Refresh` only re-renders when that boolean changes, * not on every individual query update. This overload is the only one that accepts `combine`: * ```tsx * import { Suspense } from 'react' * import { ErrorBoundary } from 'react-error-boundary' * import { * QueryErrorResetBoundary, * useSuspenseQueries, * } from '@tanstack/react-query' * * function Refresh() { * const anyFetching = useSuspenseQueries({ * queries: [ * { queryKey: ['users'], queryFn: fetchUsers }, * { queryKey: ['teams'], queryFn: fetchTeams }, * ], * combine: (results) => results.some((result) => result.isFetching), * }) * * return anyFetching ? Refreshing… : null * } * * function App() { * return ( * * {({ reset }) => ( * ( *
* There was an error! * *
* )} * > * Loading dashboard...}> * * *
* )} *
* ) * } * ``` */ declare function useSuspenseQueries, TCombinedResult = SuspenseQueriesResults>(options: { /** * An array with query option objects identical to `useSuspenseQuery`. */ queries: readonly [...SuspenseQueriesOptions] | readonly [...{ [K in keyof T]: GetUseSuspenseQueryOptions; }]; /** * 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: SuspenseQueriesResults) => TCombinedResult; }, queryClient?: QueryClient): TCombinedResult; /** * The options for `useSuspenseQueries` are the same as for `useQueries`, except that the top-level `subscribed` * option isn't supported, and each `query` can't have `throwOnError`, `enabled`, or `placeholderData`. * * @param options - The `queries` array to run in Suspense, and an optional `combine` function. * @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context * will be used. * @returns The same structure as `useQueries`, except that for each `query`, `data` is guaranteed to be * defined, `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived * flags set accordingly). * * Caveat: the component will only re-mount after all queries have finished loading. Hence, if a query has gone * stale in the time it took for all the queries to complete, it will be fetched again at re-mount. To avoid * this, make sure to set a high enough `staleTime`. Cancellation does not work. * * @example * The query error is thrown if a fetch fails and no cached data exists yet, so an error boundary is * required around ``. A failed background refetch instead continues to render the cached data. * Use {@link QueryErrorResetBoundary} to let the user retry after such an error: * ```tsx * import { Suspense } from 'react' * import { ErrorBoundary } from 'react-error-boundary' * import { * QueryErrorResetBoundary, * useSuspenseQueries, * } from '@tanstack/react-query' * * function Posts({ ids }: { ids: Array }) { * // Every result is guaranteed to be defined — no per-query `isPending` check needed. * const postQueries = useSuspenseQueries({ * queries: ids.map((id) => ({ * queryKey: ['post', id], * queryFn: () => fetchPost(id), * })), * }) * * return ( *
    * {postQueries.map((query) => ( *
  • {query.data.title}
  • * ))} *
* ) * } * * function App() { * return ( * * {({ reset }) => ( * ( *
* There was an error! * *
* )} * > * Loading posts...}> * * *
* )} *
* ) * } * ``` * * @example * Several different queries — use `useSuspenseQueries` instead of multiple `useSuspenseQuery` calls, so * they fetch in parallel rather than suspending one after another: * ```tsx * import { Suspense } from 'react' * import { ErrorBoundary } from 'react-error-boundary' * import { * QueryErrorResetBoundary, * useSuspenseQueries, * } from '@tanstack/react-query' * * function Dashboard() { * const [usersQuery, teamsQuery, projectsQuery] = useSuspenseQueries({ * queries: [ * { queryKey: ['users'], queryFn: fetchUsers }, * { queryKey: ['teams'], queryFn: fetchTeams }, * { queryKey: ['projects'], queryFn: fetchProjects }, * ], * }) * * return ( *
* * * *
* ) * } * * function App() { * return ( * * {({ reset }) => ( * ( *
* There was an error! * *
* )} * > * Loading dashboard...}> * * *
* )} *
* ) * } * ``` */ declare function useSuspenseQueries, TCombinedResult = SuspenseQueriesResults>(options: { /** * An array with query option objects identical to `useSuspenseQuery`. */ queries: readonly [...SuspenseQueriesOptions]; /** * 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: SuspenseQueriesResults) => TCombinedResult; }, queryClient?: QueryClient): TCombinedResult; //#endregion export { SuspenseQueriesOptions, SuspenseQueriesResults, useSuspenseQueries }; //# sourceMappingURL=useSuspenseQueries.d.ts.map