Repository navigation
Expand file tree
/
Copy pathuseQueries.ts
More file actions
55 lines (41 loc) · 1.57 KB
/
Copy pathuseQueries.ts
File metadata and controls
55 lines (41 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React from 'react'
import { notifyManager } from '../core/notifyManager'
import { QueriesObserver } from '../core/queriesObserver'
import { useQueryClient } from './QueryClientProvider'
import { UseQueryOptions, UseQueryResult } from './types'
export function useQueries(queries: UseQueryOptions[]): UseQueryResult[] {
const mountedRef = React.useRef(false)
const [, forceUpdate] = React.useState(0)
const queryClient = useQueryClient()
const defaultedQueries = 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
return defaultedOptions
})
const obsRef = React.useRef<QueriesObserver>()
if (!obsRef.current) {
obsRef.current = new QueriesObserver(queryClient, defaultedQueries)
}
const result = obsRef.current.getOptimisticResult(defaultedQueries)
React.useEffect(() => {
mountedRef.current = true
const unsubscribe = obsRef.current!.subscribe(
notifyManager.batchCalls(() => {
if (mountedRef.current) {
forceUpdate(x => x + 1)
}
})
)
return () => {
mountedRef.current = false
unsubscribe()
}
}, [])
React.useEffect(() => {
// Do not notify on updates because of changes in the options because
// these changes should already be reflected in the optimistic result.
obsRef.current!.setQueries(defaultedQueries, { listeners: false })
}, [defaultedQueries])
return result
}