Repository: tannerlinsley/react-query
File: packages/react-query/src/useQueries.ts (TypeScript)

Code before:
  const isRestoring = useIsRestoring()
  const errorResetBoundary = useQueryErrorResetBoundary()

  const defaultedQueries = React.useMemo(
    () =>
      queries.map((opts) => {
        const defaultedOptions = client.defaultQueryOptions(
          opts as QueryObserverOptions,
        )

        // Make sure the results are already in fetching state before subscribing or updating options
        defaultedOptions._optimisticResults = isRestoring
          ? 'isRestoring'
          : 'optimistic'

        return defaultedOptions
      }),
    [queries, client, isRestoring],
  )

  defaultedQueries.forEach((queryOptions) => {
    ensureSuspenseTimers(queryOptions)
    const query = client.getQueryCache().get(queryOptions.queryHash)
    if (query) {
      ensurePreventErrorBoundaryRetry(queryOptions, errorResetBoundary, query)
    }
  })

  useClearResetErrorBoundary(errorResetBoundary)

  const [observer] = React.useState(
    () =>
      new QueriesObserver<TCombinedResult>(
        client,
        defaultedQueries,
        options as QueriesObserverOptions<TCombinedResult>,
      ),
  )

  // note: this must be called before useSyncExternalStore
  const [optimisticResult, getCombinedResult, trackResult] =
    observer.getOptimisticResult(
      defaultedQueries,
      (options as QueriesObserverOptions<TCombinedResult>).combine,
    )

  const shouldSubscribe = !isRestoring && options.subscribed !== false
  React.useSyncExternalStore(
    React.useCallback(
      (onStoreChange) =>
        shouldSubscribe

Review comment (performance):
same here:

```suggestion
    const query = client.getQueryCache().get(queryOptions.queryHash)
    ensurePreventErrorBoundaryRetry(queryOptions, errorResetBoundary, query)
```

Code after:
  const isRestoring = useIsRestoring()
  const errorResetBoundary = useQueryErrorResetBoundary()

  const defaultedQueries = React.useMemo(
    () =>
      queries.map((opts) => {
        const defaultedOptions = client.defaultQueryOptions(
          opts as QueryObserverOptions,
        )

        // Make sure the results are already in fetching state before subscribing or updating options
        defaultedOptions._optimisticResults = isRestoring
          ? 'isRestoring'
          : 'optimistic'

        return defaultedOptions
      }),
    [queries, client, isRestoring],
  )

  defaultedQueries.forEach((queryOptions) => {
    ensureSuspenseTimers(queryOptions)
    const query = client.getQueryCache().get(queryOptions.queryHash)
    ensurePreventErrorBoundaryRetry(queryOptions, errorResetBoundary, query)
  })

  useClearResetErrorBoundary(errorResetBoundary)

  const [observer] = React.useState(
    () =>
      new QueriesObserver<TCombinedResult>(
        client,
        defaultedQueries,
        options as QueriesObserverOptions<TCombinedResult>,
      ),
  )

  // note: this must be called before useSyncExternalStore
  const [optimisticResult, getCombinedResult, trackResult] =
    observer.getOptimisticResult(
      defaultedQueries,
      (options as QueriesObserverOptions<TCombinedResult>).combine,
    )

  const shouldSubscribe = !isRestoring && options.subscribed !== false
  React.useSyncExternalStore(
    React.useCallback(
      (onStoreChange) =>
        shouldSubscribe
          ? observer.subscribe(notifyManager.batchCalls(onStoreChange))
          : noop,