| 353 | * ``` |
| 354 | */ |
| 355 | export 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) |