Skip to content

Commit

Permalink
fix(useQueries): add useMemo to useQueries to keep result stable (#2992)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecf committed Nov 24, 2021
1 parent f18881d commit c6906b3
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/react/useQueries.ts
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useMemo } from 'react'
import { QueryFunction } from '../core/types'

import { notifyManager } from '../core/notifyManager'
Expand Down Expand Up @@ -118,14 +118,20 @@ export function useQueries<T extends any[]>(

const queryClient = useQueryClient()

const defaultedQueries = queries.map(options => {
const defaultedOptions = queryClient.defaultQueryObserverOptions(options)
const defaultedQueries = useMemo(
() =>
queries.map(options => {
const defaultedOptions = queryClient.defaultQueryObserverOptions(
options
)

// Make sure the results are already in fetching state before subscribing or updating options
defaultedOptions.optimisticResults = true
// Make sure the results are already in fetching state before subscribing or updating options
defaultedOptions.optimisticResults = true

return defaultedOptions
})
return defaultedOptions
}),
[queries, queryClient]
)

const [observer] = React.useState(
() => new QueriesObserver(queryClient, defaultedQueries)
Expand Down

1 comment on commit c6906b3

@vercel
Copy link

@vercel vercel bot commented on c6906b3 Nov 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.