forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactQueryCacheProvider.js
More file actions
37 lines (30 loc) · 1013 Bytes
/
ReactQueryCacheProvider.js
File metadata and controls
37 lines (30 loc) · 1013 Bytes
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
import React from 'react'
import { queryCache, makeQueryCache } from '../core'
export const queryCacheContext = React.createContext(queryCache)
export const queryCaches = [queryCache]
export const useQueryCache = () => React.useContext(queryCacheContext)
export function ReactQueryCacheProvider({ queryCache, children }) {
const resolvedQueryCache = React.useMemo(
() => queryCache || makeQueryCache(),
[queryCache]
)
React.useEffect(() => {
queryCaches.push(resolvedQueryCache)
return () => {
// remove the cache from the active list
const i = queryCaches.indexOf(resolvedQueryCache)
if (i > -1) {
queryCaches.splice(i, 1)
}
// if the resolvedQueryCache was created by us, we need to tear it down
if (queryCache == null) {
resolvedQueryCache.clear()
}
}
}, [resolvedQueryCache, queryCache])
return (
<queryCacheContext.Provider value={resolvedQueryCache}>
{children}
</queryCacheContext.Provider>
)
}